errgroup is a Go module that provides Context cancellation, error propagation and synchronisation
for goroutines running fallible functions.
As per the Go proverb, the zero value of the errgroup.Group is useful, and you can simply create
a errgroup.Group as follows and begin using it:
var eg errgroup.GroupYou can also configure the behaviour of an errgroup.Group by providing some errgroup.Configurer's to the
errgroup.New function. For example:
var (
ctx, cc = errgroup.WithCancel(ctx.Background())
eg = errgroup.New(cc)
)Once you have created an errgroup.Group, you can begin using it by calling errgroup.Group.Go. Then,
you can call errgroup.Group.Wait to wait for the result:
var fs []func() error
// ...
for _, f := range fs {
_ = eg.Go(f)
}
errs := eg.Wait()
fmt.Println(errs)Documentation for errgroup can be found here.