运行 deadlock
package main import ( "fmt" ) func main() { ch := make(chan string) for i := 0; i < 5; i++ { go printHello(i, ch) } for { message := <-ch fmt.Println(message) } } func printHello(i int, ch chan string) { message := fmt.Sprintf("groutine %d send u message", i) fmt.Println(message) ch <- message }
以上是我运行的代码,执行结果:
fatal error: all goroutines are asleep - deadlock! groutine 4 send u message groutine 4 send u message goroutine 1 [chan receive]: groutine 1 send u message main.main() groutine 1 send u message groutine 0 send u message /Users/donng/go/src/practice/cmd/hello-world/main.go:14 +0x9e groutine 2 send u message