问题代码
1 | package main |
- 创建一个无缓存channel chanStr
- 往chanStr放入int类型1
- 从chanStr读取内容至变量a
- 打印变量a
看着没有什么问题,逻辑也很正确,但结果发生了报错
1 | fatal error: all goroutines are asleep - deadlock! |
产生报错的原因
官方在这边说明了:
- Receivers always block until there is data to receive. (直到channel内存在数据,消费端才会解除block状态)
- If the channel is unbuffered, the sender blocks until the receiver has received the value.(如果是无buff的channel,直到消费者消费了数据,发送者才会解除block状态)
- If the channel has a buffer, the sender blocks only until the value has been copied to the buffer.(如果是有buff的channel,那么在发送者发送数据到channel之前是block状态)
- if the buffer is full, this means waiting until some receiver has retrieved a value.(如果buff满了,则当消费者消费数据之前,会一直等待)
产生的问题是第二点,main本身就是个goroutine,所以执行到chanStr <-1
因为没有对应的消费者消费数据,所以被block,下面的代码也就无法执行,导致deadlock错误。
解决方法
将a := <- chanStr
单独作为一个goroutine,这样就存在两个goroutine,一个main,还有一个是匿名函数,接受者和发送者在不同goroutine上,就不会出现deadlock了。
1 | package main |