Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Concurrency and Context

Go sync.WaitGroup type

Reference for Go sync.WaitGroup: Add, Done, Wait and the Go method (Go 1.25), fan-out patterns, collecting results safely and common counter mistakes.

A sync.WaitGroup waits for a collection of goroutines to finish. It keeps a counter: Add(n) raises it, Done() lowers it by one, and Wait() blocks until the counter reaches zero. Without it, main would return while goroutines are still running, and the program would exit and kill them.

Since Go 1.25 there is also wg.Go(f), which does the Add(1), starts a goroutine running f, and calls Done when f returns. It removes the most common mistakes in older code.

Go
var wg sync.WaitGroup
wg.Go(func() { fmt.Println("working") })
wg.Wait()

Try it

Go The classic Add, Done, Wait pattern

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go wg.Go and a mutex-protected result

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
type WaitGroup struct{ /* ... */ }

func (wg *WaitGroup) Add(delta int)
func (wg *WaitGroup) Done()
func (wg *WaitGroup) Wait()
func (wg *WaitGroup) Go(f func())
Method Description
Add(delta) Adds delta (possibly negative) to the counter.
Done() Same as Add(-1).
Wait() Blocks until the counter is zero. Several goroutines may wait.
Go(f) Increments the counter and runs f in a new goroutine; decrements when f returns. Go 1.25.

Panics: the counter going negative panics ("sync: negative WaitGroup counter"). Misusing Add concurrently with Wait when the counter is zero can panic or race.

Usage notes

  • Call Add before go, in the goroutine that will call Wait. If the new goroutine calls Add itself, Wait might run first, see zero, and return too early.
  • Always defer wg.Done() so a panic or early return still decrements the counter. A missed Done makes Wait block forever.
  • Pass the group by pointer to functions: func worker(wg *sync.WaitGroup). A copy has its own counter and Wait never sees the Done calls. go vet catches this.
  • Reuse is allowed once Wait has returned, but new Add calls must happen after all previous Wait calls return.
  • Loop variables are per iteration since Go 1.22, so go func() { use(i) }() inside a for loop is correct, as in the first example. Older code passed i as an argument.
  • Collecting results: write to distinct slice indexes (no lock needed), use a mutex, or send into a channel that another goroutine reads. Do not append to a shared slice without a lock.
  • No error handling or cancellation. WaitGroup only counts. For the first error and cancellation of siblings, use errgroup from golang.org/x/sync or a context.
  • Limit concurrency with a buffered channel used as a semaphore when you have thousands of tasks.