這是代碼:import "fmt"func main() { messages := make(chan string, 1) go func(c chan string) { c <- "Hi" }(messages) select { case msg := <-messages: fmt.Println("received message", msg) default: fmt.Println("no message received") }}它輸出no message received. 或者這段代碼:import ( "fmt" "time")func f(from string) { for i := 0; i < 3; i++ { fmt.Println(from, ":", i) }}func main() { go f("goroutine") go func(msg string) { fmt.Println(msg) }("going") time.Sleep(time.Second) fmt.Println("done")}意外打印goinggoroutine : 0goroutine : 1goroutine : 2盡管 goroutine 的going調(diào)用晚于計數(shù)器。為什么?
goroutine 調(diào)用行之后的行是否比 goroutine 的第一行更早開始?
慕無忌1623718
2022-06-01 18:18:53