当
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


