(并发编程新手) 有疑问为什么 goroutine 的执行流程在这里有点奇怪?
Golang 中 goroutine 和 channel 的初学者。
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
}
最佳答案
关于go - 给定代码的执行流程将如何? go 例程在这里将如何执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58692089/