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.Group
However, if you would like to construct an errgroup.Group
from some configuration, you can use
the errgroup.New
function and supply some errgroup.Configurer
's:
var (
ctx, cc = errgroup.WithCancel(ctx.Background())
lc = errgroup.WithLimit(10)
eg = errgroup.New(cc, lc)
)
Once you've created an errgroup.Group
, you can begin using it by calling errgroup.Group.Go
(or
errgroup.Group.TryGo
). Then, errgroup.Group.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.