import "golang.org/x/sync/errgroup"
func TestErrorGroup(t *testing.T) {
g, ctx := errgroup.WithContext(context.Background())
g.Go(func() error {
time.Sleep(time.Microsecond)
return errors.New("test 1")
})
g.Go(func() error {
return errors.New("test 2")
})
g.Go(func() error {
return errors.New("test 3")
})
err := g.Wait() // 返回所有err中的一个
fmt.Println(err)
fmt.Println(ctx.Err())
}
errgroup
于 2021-10-10 18:53:19 首次发布