liveBook · Manning

Errgroup

As explained in Learn Go with Pocket-Sized Projects

errgroup is a Go package for synchronizing goroutines while collecting errors from their functions.

errgroup synchronizes goroutines while collecting function errors, avoiding the error channel required when combining sync.WaitGroup with concurrent work. section 7.2.4

Use an errgroup.Group to:

  • Launch work with Go, passing functions that return errors. section 7.2.4
  • Limit the number of goroutines running simultaneously with SetLimit(n). section 7.2.4
  • Wait for the group and handle the returned error. section 7.2.4

Cooking rice and curry concurrently

var g errgroup.Group g.SetLimit(2) g.Go(func() error { cookRice(); return nil }) g.Go(cookCurry) if err := g.Wait(); err != nil { /* handle error */ }

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains Group creation, Go, SetLimit, and Wait, but defers detailed context treatment.
Examples
Demonstrates concurrent cooking of rice and curry with a two-goroutine limit.
What it assumes
Assumes familiarity with goroutines and sync.WaitGroup, while introducing error-channel elimination through a library.
What you can do after
Operate golang.org/x/sync/errgroup to launch limited concurrent work and handle errors returned by Wait.
How it connects
Connects goroutines, sync.WaitGroup, error channels, context.Context, and synchronization.

Also explained in

Compare how 2 books explain errgroup →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.