<分区>
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(1000 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
运行代码,输出为:
hello
world
hello
world
hello
world
hello
world
hello
在前 4 个循环中,每 100 毫秒,将打印一个“hello”,然后打印一个“world”。并且在最后一个循环中只会打印一个“hello”。
有没有人可以解释一下代码的执行顺序是什么?