sync.WaitGroup: wait for a group of goroutines to finish
Answer: A WaitGroup is a counting semaphore used to wait for goroutines or tasks to finish. Start the work, then call Wait(). A WaitGroup must not be copied after first use.
Waiting for a group of tasks
A WaitGroup is a counting semaphore used to wait for goroutines or tasks to finish.
A main goroutine can start tasks, each in a new goroutine, by calling WaitGroup.Go, then wait for all tasks to complete by calling WaitGroup.Wait.
The older Add and Done pattern
Add and Done can track tasks when you do not use Go to start new goroutines.
The same work can use explicitly created goroutines along with Add and Done.
This pattern is common in code that predates WaitGroup.Go.
Copying rules in the source
Both the package docs and the waitgroup.go comments say a WaitGroup must not be copied after first use.
This pattern is common in code that predates WaitGroup.Go.
See also
Claims
- id: C1 claim: A WaitGroup is a counting semaphore for waiting on a group of tasks. source: S1 evidence: "A WaitGroup is a counting semaphore typically used to wait"
- id: C2 claim: WaitGroup.Go starts a task; Wait waits until the group is done. source: S1 evidence: "by calling WaitGroup.Go and then wait for all tasks to complete"
- id: C3 claim: Add and Done can track work you start yourself. source: S1 evidence: "using WaitGroup.Add and WaitGroup.Done"
- id: C4 claim: A WaitGroup must not be copied after first use. source: S2 evidence: "A WaitGroup must not be copied after first use"
- id: C5 claim: The Add and Done pattern predates WaitGroup.Go. source: S2 evidence: "This pattern is common in code that predates"
Sources
- [S1] sync package reference. Go. https://pkg.go.dev/sync . License: BSD-3-Clause. Accessed 2026-11-23.
- [S2] sync/waitgroup.go source. Go. https://go.dev/src/sync/waitgroup.go . License: BSD-3-Clause. Accessed 2026-11-23.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go, sync package reference; Go, sync/waitgroup.go source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go, sync package reference; Go, sync/waitgroup.go source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "sync.WaitGroup: wait for a group of goroutines to finish." iinventedlight Library, iil:note:a5psn5ap, version of 2026-11-23. https://iinventedlight.com/library/notes/go-sync-waitgroup/. License: CC-BY-4.0.
@misc{iil_note_a5psn5ap,
author = {{iinventedlight}},
title = {{sync.WaitGroup: wait for a group of goroutines to finish}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/go-sync-waitgroup/},
note = {Permanent ID iil:note:a5psn5ap. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-sync-waitgroup.schema.json (schema; CC-BY-4.0)
- go-sync-waitgroup.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-sync-waitgroup.md.