栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

goroutine没有输出

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

goroutine没有输出

main()
函数结束时,程序也将结束。它不等待其他goroutine完成。

引用Go语言规范:程序执行:

程序执行首先初始化主程序包,然后调用函数

main
。当该函数调用返回时,程序退出。它不等待其他(非
main
)goroutine完成。

有关更多详细信息,请参见此答案。

您必须告诉您的

main()
函数等待以
SayHello()
goroutine形式启动的函数完成。您可以将它们与通道同步,例如:

func SayHello(done chan int) {    for i := 0; i < 10; i++ {        fmt.Print(i, " ")    }    if done != nil {        done <- 0 // Signal that we're done    }}func main() {    SayHello(nil) // Passing nil: we don't want notification here    done := make(chan int)    go SayHello(done)    <-done // Wait until done signal arrives}

另一种选择是通过关闭通道来发出完成信号:

func SayHello(done chan struct{}) {    for i := 0; i < 10; i++ {        fmt.Print(i, " ")    }    if done != nil {        close(done) // Signal that we're done    }}func main() {    SayHello(nil) // Passing nil: we don't want notification here    done := make(chan struct{})    go SayHello(done)    <-done // A receive from a closed channel returns the zero value immediately}

笔记:

根据您的编辑/评论:如果您希望2个运行

SayHello()
函数随机打印“混合”数字:您不能保证观察到这种行为。同样,请参阅上述答案以获取更多详细信息。在
转到内存模型
只能保证某些事件发生的其他事件之前,你有没有保证2个并发够程是如何执行的。

您可以尝试使用它,但是知道结果将不确定。首先,您必须启用多个活动goroutine才能执行:

runtime.GOMAXPROCS(2)

其次,您必须首先

SayHello()
以goroutine身份启动,因为当前代码首先
SayHello()
在主goroutine中执行,并且只有在完成后才启动另一个goroutine:

runtime.GOMAXPROCS(2)done := make(chan struct{})go SayHello(done) // FIRST START goroutineSayHello(nil) // And then call SayHello() in the main goroutine<-done // Wait for completion


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/379396.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号