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
- goroutine — A function running concurrently with the rest of a Go program, started by putting the go keyword before a call.
- channel — A typed pipe in Go that one goroutine sends values into and another receives them from, safely handing data between them.
- defer — A Go statement that schedules a function call to run when the surrounding function returns, used for cleanup such as closing a file.
- mutex — A lock that lets only one piece of code at a time run a critical section or do a particular piece of work.
Taught in
- Goroutines and Concurrent Execution — Concurrent Systems Programming with Go
- Executing Concurrent Checks — Concurrent Systems Programming with Go