相关疑难解决方法(0)

等待一个例程完成的正确方法

我想知道在退出程序之前等待程序完成的正确方法是什么.阅读其他一些答案似乎bool chan会做到这一点,就像Playground链接一样

func do_stuff(done chan bool) {
    fmt.Println("Doing stuff")
    done <- true
}

func main() {
    fmt.Println("Main")
    done := make(chan bool)
    go do_stuff(done)
    <-done
    //<-done
}
Run Code Online (Sandbox Code Playgroud)

我这里有两个问题:

  • 为什么< - 完成工作?

  • 如果我取消注释最后一行会发生什么?我有一个死锁错误.这是因为频道是空的,没有其他功能向它发送值?

channel go goroutine

3
推荐指数
2
解决办法
5507
查看次数

标签 统计

channel ×1

go ×1

goroutine ×1