(并發(fā)編程新手)有疑問為什么 goroutine 的執(zhí)行流程在這里有點奇怪?golang 中 goroutine 和通道的初學(xué)者。func main() { // Set up the pipeline. c := gen(2, 3) out := sq(c) // Consume the output. fmt.Println(<-out) // 4 fmt.Println(<-out) // 9}func sq(in <-chan int) <-chan int { out := make(chan int) go func() { for n := range in { out <- n * n } close(out) }() return out}func gen(nums ...int) <-chan int { out := make(chan int) go func() { for _, n := range nums { out <- n } close(out) }() return out}
給定代碼的執(zhí)行流程將如何?go 例程在這里將如何執(zhí)行?
慕尼黑8549860
2023-07-31 16:56:44