我需要并行运行多个函数。
如果函数返回true
(在通道上发送true
),那么最终结果应该是true
。与golang中的通道卡住
如何使用goroutines和通道实现此目的?
// Some performance intensive function
func foo(i int, c chan bool) {
// do some processing and return either true or false
c <- true // or false
}
func main() {
flg := false
ch := make(chan bool)
for i := 0; i < 10; i++ {
go foo(i, ch)
}
// If even once foo() returned true then val should be true
flg = flg || <-ch
}
“我如何使用频道实现此目标?” ---你有什么理由必须使用它的渠道? – zerkms
请向我们展示您的尝试。你的代码根本不使用频道。 – Flimzy
(同样,你最好使用'gofmt',因为你的代码不容易阅读) – Flimzy