【发布时间】:2017-03-06 23:27:40
【问题描述】:
我是 Go 语言编程的新手,并逐步学习它。
在练习的过程中,我发现了 goroutines 的随机行为。
如果我调用 goroutine(睡眠时间为 1 秒的函数),有时它会成功完成,有时它不会:
package main
import (
"fmt"
"time"
)
func t(i int) {
fmt.Println("In func t")
time.Sleep(1)
}
func t1(i int) {
fmt.Println("In func t1")
time.Sleep(1)
}
func main() {
fmt.Println("Hello Good Morning")
go t(1)
t1(2)
time.Sleep(5)
fmt.Println("End of func main")
}
O/p 1:
Hello Good Morning
In func t1
In func t
End of func main
O/p 2:
Hello Good Morning
In func t1
End of func main
谁能解释一下为什么 goroutine 不能保证 goroutine 函数调用的执行。
【问题讨论】: