草庐IT

goruntine

全部标签

go - 为什么这段关于golang goruntine运行顺序的代码首先是 "2"

packagemainimport("fmt""sync")funcmain(){runtime.GOMAXPROCS(1)w:=&sync.WaitGroup{}w.Add(2)gofunc(){fmt.Println("1")w.Done()}()gofunc(){fmt.Println("2")w.Done()}()w.Wait()}https://play.golang.org/p/ESi1mKAo1x_S呃,我不知道为什么先打印“2”。我想查资料,但不知道查什么资料,所以发帖求助我认为第一个goroutine是队列中的第一个推送。它应该首先打印。

go - 为什么这段关于golang goruntine运行顺序的代码首先是 "2"

packagemainimport("fmt""sync")funcmain(){runtime.GOMAXPROCS(1)w:=&sync.WaitGroup{}w.Add(2)gofunc(){fmt.Println("1")w.Done()}()gofunc(){fmt.Println("2")w.Done()}()w.Wait()}https://play.golang.org/p/ESi1mKAo1x_S呃,我不知道为什么先打印“2”。我想查资料,但不知道查什么资料,所以发帖求助我认为第一个goroutine是队列中的第一个推送。它应该首先打印。