标签:
func Count(ch chan int) { counter++ ch <- counter fmt.Println("Counting") } func main() { chs := make([]chan int, 10) for i := 0; i < 10; i++ { chs[i] = make(chan int) go Count(chs[i]) } for index, ch := range chs { fmt.Println(index, "", <-ch) } } 输出一致结果: Counting 0 1 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10
猜测是range方法读取channel (chs)时候先读取第一个,然后再读取剩余全部,如果剩余管道缓冲数据没有填满N,则阻塞
另外一个种方案:
func Count(ch chan int) { ch <- counter counter++ fmt.Println("Counting") } 输出一致结果: Counting 0 0 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1
标签:
原文地址:http://my.oschina.net/u/932809/blog/481533