← All notes

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

Sources

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

Permanent ID: iil:note:a5psn5ap

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.},
}

Markdown

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.

All artifact files (JSON index)

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.