用chan接收数据后,打印输出会出现deadlock
func main() { ch := make(chan string) for i := 0; i < 300; i++ { go PrintHello(i, ch) } for { msg := <-ch fmt.Println(msg) } } func PrintHello(i int, ch chan string) { ch <- fmt.Sprintf("print %d\n", i) }
运行后:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
~/simpleGoRoutine.go:15 +0xa1
print 0
print 2
...