dongzhan1383 2018-06-23 14:38
浏览 16

转到频道未死锁或锁定

I have a small snippet of code like this

func main() {
    var c chan string
    go func() {
        c <- "let's get started"//write1
        //c <- "let's get started"//write2
        //c <- "let's get started"//write3
        fmt.Println("wrote the stuff....")
    }()
    //time.Sleep(3 * time.Second) //adding this always shows fatal exception
    c = make(chan string)
    fmt.Println(<-c)
}

I do not see the output wrote the stuff.... on console if I uncomment //write2 and write3 code snippet lines. I understand that probably I don't see it is because the channel is unbuffered and totally synchronous, and there is only one read happening outside the channel. However, when program exits, the go routine is blocked, why is there no error like deadlocked... or something in this case that I see?

  • 写回答

2条回答 默认 最新

  • douzhi7661 2018-06-23 14:41
    关注

    Create the channel before you try to write to it. If the func goroutine tries to send elements through c before there's actually a channel there, it ends up using a nil channel, which blocks forever.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集