當(dāng)我運(yùn)行以下代碼時(shí)沒(méi)有錯(cuò)誤,但也沒(méi)有打印任何內(nèi)容:package mainimport "fmt"func fibonacci(c, quit chan int) { x, y := 0, 1 counter := 0 for { select { case c <- x: fmt.Println("sent x", x) x, y = y, x+y case <-quit: fmt.Println("quit") return } counter += 1 fmt.Println("Counter", (counter)); } fmt.Println("Fib exiting");}func main() { c := make(chan int) quit := make(chan int) go func() { for i := 0; i < 10; i++ { fmt.Println("got" , <-c) } quit <- 0 fmt.Println("Anon exiting"); }() go fibonacci(c, quit)}我在想至少應(yīng)該打印出來(lái)。如果我在主歌唱中正常執(zhí)行,它就會(huì)起作用。"Fib exiting"fibonacci()提前致謝...
難道不能保證所有語(yǔ)句都在一個(gè) goroutine 中執(zhí)行嗎?
明月笑刀無(wú)情
2022-08-24 20:34:05