packagemainfuncmain() {var ch chanbool ch <-true// deadlocks because ch is nil}---------Output-----------fatal error: all goroutines are asleep - deadlock!
通道没有初始化值则为nil,所以上述程序会永久阻塞(发生死锁)。
上述代码并无利用价值,它属于缺陷(Bug)。请不要这样做!
从nil通道接收数据会永久阻塞
packagemainimport"fmt"funcmain() {var ch chanbool fmt.Printf("Value received from ch is: %v\n", <-ch) // deadlock because ch is nil}--------Output---------fatal error: all goroutines are asleep - deadlock!