Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 1.11 KB

README.md

File metadata and controls

50 lines (36 loc) · 1.11 KB

errgroup

Overview

errgroup is a Go module that provides Context cancellation, error propagation and synchronisation for goroutines running fallible functions.

Usage

Creating an errgroup

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)
)

Using an errgroup

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

Documentation for errgroup can be found here.