下面这段代码为什么会产生错误?这段代码为什么会产生一个错误?
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