API Reference

errgroup

Synchronization primitive for goroutine groups in Go that propagates the first error and cancels peers.

Official Documentation ↗
CompatibilityThis documentation covers version golang.org/x/sync. Check the official docs for the latest API surface.

Installation

gogo get golang.org/x/sync/errgroup

Methods

Synchronization primitive for goroutine groups in Go that propagates the first error and cancels peers.

Overview

errgroup packages the fan-out pattern Go repeats everywhere: launch N goroutines, gather their work, and surface the first failure. Group.Go registers each task, and Group.Wait blocks until all complete, returning the earliest error.

WithContext binds the group to a derived context canceled on first failure, so sibling goroutines observe ctx.Done() and stop early. SetLimit turns the same structure into a bounded worker pool without extra channels.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • g.Go — Launches a goroutine tracked by the group; a non-nil error is stored for Wait to return.
  • errgroup.WithContext — Creates a group whose derived context is canceled as soon as any member returns an error.
  • g.SetLimit — Caps concurrent goroutines in the group; Go blocks until a slot frees when the limit is reached.
  • g.Wait — Blocks until every goroutine in the group finishes, returning only the first error encountered.

Usage Notes

  • Use WithContext whenever members do meaningful work, and check ctx inside loops so cancellation is prompt.
  • Wrap returned errors with %w at the call site to preserve the failure chain.
  • SetLimit from a measured downstream budget, database connections or API quotas, not an arbitrary number.

Common Pitfalls

  • Capturing the loop variable in g.Go without per-iteration copies sends every goroutine the last item.
  • Wait returns the first error only; silently losing other failures hides cascading causes.
  • With plain Group (no context) members keep running after one fails, wasting work.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://pkg.go.dev/golang.org/x/sync/errgroup · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:49:27.948Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

Why does Wait return only one error?
The group stores the first error and drops later ones by design, because that error already caused cancellation via WithContext. Collect errors yourself in a mutex-protected slice if you need every failure.
How is errgroup different from sync.WaitGroup?
WaitGroup counts completations but knows nothing about errors or cancellation. errgroup tracks error state, cancels a shared context on failure, and Wait returns the error instead of requiring manual plumbing.
Does SetLimit apply to queued or running goroutines?
It caps goroutines started but not yet finished: Go blocks when the limit is reached until a running member completes. It is a semaphore over active work, not a queue with backlog semantics.