下面這段代碼爲什麼會產生錯誤?這段代碼爲什麼會產生一個錯誤?
func main() {
messages := make(chan string)
messages <- "test" //line 16
fmt.Println(<-messages)
}
生成以下錯誤。
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.main()
/tmp/sandbox994400718/main.go:16 +0x80
將值發送到通道,並在下一行中接收值。技術上它應該工作。
我得到的,但怎麼就是這個例子http://play.golang.org/p/2ox5ObgKIP不需要緩衝。 – nJoshi
@nJoshi在那個例子中有_two_ goroutines。 goroutine以'go func()...'開始,將字符串傳遞給通道(並等待),主要goroutine檢索它(並允許另一個繼續)。 – matt