sync.WaitGroup

A counter from Go's sync package that lets one goroutine wait until a group of other goroutines have all finished.

In more detail

Call Add(1) before starting each goroutine, have each goroutine call Done (usually with defer), and call Wait to block until the count returns to zero. It is the standard way to join workers before closing a results channel or exiting.

Example

var wait sync.WaitGroup
wait.Add(1)
go func() { defer wait.Done() }()
wait.Wait()

Related terms

Taught in

References