有人可以解释一下,为什么如果通道被缓冲,程序不会以fatal_error退出?缓冲/无缓冲通道
无缓冲通道
package main
func main() {
c := make(chan int)
c <- 3
}
fatal error: all goroutines are asleep - deadlock!
缓冲通道
package main
func main() {
c := make(chan int, 1)
c <- 3
}
[no output]
Program exited.
谢谢!
我想这是因为缓冲和非缓冲通道之间的差异。在缓冲的通道中,发送方等待接收方(本例中为自己)获取数据。但我不知道.. –
可能的重复[如何使(chan布尔)行为不同于make(chan bool,1)?](http://stackoverflow.com/questions/20041392/how-does-makechan -bool-表现-不同地从-makechan-布尔-1) – Matt