关于chan的疑问
package main import ( "fmt" ) func main() { ch := make(chan string) for i := 0; i <= 5; i++ { go printHelloWorld(i, ch) } for { if msg,ok := <-ch;ok{ fmt.Println(msg) }else{ break } } } func printHelloWorld(i int, ch chan string) { ch <- fmt.Sprintf("hello world %d\n", i) }
老师:为什么我这段代码输出会有个
fatal error: all goroutines are asleep - deadlock!
报错呢?