1. 首页
  2. 主题
  3. Go语言

如果直接在main方法中读取会出现error,如果使用go receive方式程序正常

does · · 1243 次点击
![ read chan by for range in main-thread.png](https://static.studygolang.com/190217/615aabb98cdf18b6e24423e6c25969a5.png)
o,还得注意,关闭channel 的操作不能和读取channel 的操作在一个协程里
#5
更多评论
因为如果放在main中,会从channel中读数据,没有读到就会死锁。
#1
@waini1110</a> #### 首先感谢回答,另外: ##### 对于读取来说应该是发现channel有数据就拿 ,没有数据就阻塞 ##### 对于发送来说向channel中存,存完阻塞,等待channel数据被取走后再存 ##### 现在我在main中不断读取channel, 假设channel不存在数据,他应该只是阻塞状态,为什么会提示 all goroutines are asleep - deadlock
#2

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中