Goroutine缓冲了通道死锁



在主goroutine中,如果我向超出容量的通道发送值,我会出现死锁,但如果我在不同的goroutine发送,则不会出现死锁,为什么?

func main() {
c := make(chan int, 2)
for i := 0; i < 3; i++ {
c <- i
}
}
func main() {
c := make(chan int, 2)
go func() {
for i := 0; i < 3; i++ {
c <- i
}
close(c)
}()
time.Sleep(5 * time.Second)
}
func main() {
c := make(chan int, 2)
for i := 0; i < 3; i++ {
// Blocking operation
c <- i
}
}
By default >>>sends<<< and receives block(!) until both the sender and receiver are ready

签出-https://gobyexample.com/channels

您的频道上没有接收器,主goroutine(=发送器(被阻止

最新更新