Closing channel of unknown length
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm not able to close channel when there is no knowledge about its length package main import ( "fmt" "time" ) func gen(ch chan int) { var i int for { time.Sleep(time.Millisecond * 10) ch <- i i++ // when no more data (e.g. from db, or event stream) if i > 100 { break } } // hot to close it properly? close(ch) } func receiver(ch chan int) { for i := range ch { fmt.Println("received:", i) } } func main() { ch := make(chan int) for i := 0; i < 10; i++ { go gen(ch) } receiver(ch) } It gives me error panic: send on closed channel goroutine 8