Why this matters
Prevents accidental serialization and data races from captured variables.
Launch goroutines outside of waits, capture loop variables, and use sync.WaitGroup (and buffered channels when needed) to achieve true concurrency.
Prevents accidental serialization and data races from captured variables.
Side-by-side examples engineers can pattern-match during review.
for _, u := range urls {
go func() { fetch(u) }() // captures loop var
}var wg sync.WaitGroup
wg.Add(len(urls))
for _, u := range urls {
u := u // capture
go func(u string){
defer wg.Done()
fetch(u)
}(u)
}
wg.Wait()go func(){ use(u) }() // without capturing uu := u; go func(u string){ ... }(u)From the same buckets as this rule.