【发布时间】:2017-04-03 09:23:50
【问题描述】:
我在看https://blog.golang.org/pipelines的例子:
func main() {
in := gen(2, 3)
// Distribute the sq work across two goroutines that both read from in.
c1 := sq(in)
// When does this line below execute and what is in `in`?
c2 := sq(in)
// Consume the merged output from c1 and c2.
for n := range merge(c1, c2) {
fmt.Println(n) // 4 then 9, or 9 then 4
}
}
c2 := sq(in) 何时运行?据我了解,它不是在上一行完成时执行,而是立即执行,因为那是一个 goroutine。
c2 会收到在 c1 收到的消息之后的下一条传入消息吗?
【问题讨论】:
-
不,它在上一行完成时执行。没有
go,所以它不是一个goroutine。 (gen函数可以启动一个 goroutine,但它是完全独立的并且在后台运行。)