duanao3204 2015-10-08 15:52
浏览 14
已采纳

goroutine仅通过通道传递值的一半

I have a file:

package main
import "fmt"

func
combinations(result chan []byte, len int, min byte, max byte) {
    res := make([]byte,len)
    for i := 0; i < len; i++ {
        res[i] = min
    }
    result <- res
    for true {
        i := 0
        for i = 0; i < len; i++ {
            if res[i] < max {
                res[i] = res[i] + 1;
                break
            } else {
                res[i] = 32
            }
        }
        result <- res
        if(i == len) {
            close(result)
            return;
        }
    }
}

func
main() {
    combination_chan := make(chan []byte)
    go combinations(combination_chan, 2, 0, 5)
    for next_combination := range combination_chan {
        fmt.Printf("%x
",next_combination)
    }
}

I expect this to print all possible combinations of 2 bytes between 0 and 5, IE:

0000
0100
...
0001
...
0505

However, it seems to skip every other value, and print the same value twice, IE:

0100
0100
0300
0300
...

Why would it be doing this? I've inserted prints before the 'result <- res' line, and those are all correct.

  • 写回答

1条回答 默认 最新

  • dpauf28808 2015-10-08 16:09
    关注

    If we simplify a bit, a slice in Go is basically a pointer to an array, so by passing a slice that you still own and modify through a channel, you create a data race.

    There is no telling if the contents of the slice are modified between the moment it is passed to the channel and the moment it is read from the channel by another goroutine.

    So your whole algorithm leads to undefined behaviour, because you only pass the same contents over and over again while modifying them.

    A solution in your case would be to copy the slice before sending it through the channel:

    buf := make([]byte, len(res))
    copy(buf, res)
    result <- buf
    

    See it running here: http://play.golang.org/p/ulLYy9Uqnp

    Also, I don't recommend using len as a variable name, because it can conflict with the len() builtin function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向