Canonical guidance

Use when

Avoid

Preferred pattern

var wg sync.WaitGroup

for _, job := range jobs {
	wg.Add(1)
	go func(job Job) {
		defer wg.Done()
		process(job)
	}(job)
}

wg.Wait()

Anti-pattern

Explanation: This anti-pattern is tempting because the goroutine already “knows” it exists, but WaitGroup correctness depends on registration happening first.

Why

Related pages

Sources