我正在尝试sync.WorkGroup使用通道启动多个 go-routines,并从这些工作人员那里收集错误。(这是一个好方法吗?)。每当我尝试写入错误通道时,程序似乎都会停止。无法弄清楚它的原因。
场景看起来像这样 -
package main
import (
"fmt"
"time"
)
func worker(id int, i int, errs chan<- errs) {
defer wg.Done()
errs <- fmt.Errorf("Sample Error") // This is where the executions seems to be stuck
return
}
func main() {
errs := make(chan int)
var wg sync.WaitGroup
for w := 1; w <= 3; w++ {
wg.Add(1)
go worker(w, w, errs)
}
wg.Wait()
fmt.Printf("Hello, all tasks done.") // Never executed.
close(errs)
for err := range errs{
fmt.Printf("%s", err.Error())
}
}
慕标琳琳
相关分类