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.
var wg sync.WaitGroup wg.Go(func() { fmt.Println("working") }) wg.Wait()
Try it
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.
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
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
Addbeforego, in the goroutine that will callWait. If the new goroutine callsAdditself,Waitmight run first, see zero, and return too early. - Always
defer wg.Done()so a panic or early return still decrements the counter. A missedDonemakesWaitblock forever. - Pass the group by pointer to functions:
func worker(wg *sync.WaitGroup). A copy has its own counter andWaitnever sees theDonecalls.go vetcatches this. - Reuse is allowed once
Waithas returned, but newAddcalls must happen after all previousWaitcalls return. - Loop variables are per iteration since Go 1.22, so
go func() { use(i) }()inside aforloop is correct, as in the first example. Older code passedias 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.
WaitGrouponly counts. For the first error and cancellation of siblings, useerrgroupfromgolang.org/x/syncor acontext. - Limit concurrency with a buffered channel used as a semaphore when you have thousands of tasks.
Related
sync,sync.Mutex,go,chan,context- Lessons: Goroutines, The sync package, Concurrency patterns