Source file src/cmd/vendor/golang.org/x/tools/go/analysis/passes/waitgroup/doc.go
1 // Copyright 2024 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package waitgroup defines an Analyzer that detects simple misuses 6 // of sync.WaitGroup. 7 // 8 // # Analyzer waitgroup 9 // 10 // waitgroup: check for misuses of sync.WaitGroup 11 // 12 // This analyzer detects mistaken calls to the (*sync.WaitGroup).Add 13 // method from inside a new goroutine, causing Add to race with Wait: 14 // 15 // // WRONG 16 // var wg sync.WaitGroup 17 // go func() { 18 // wg.Add(1) // "WaitGroup.Add called from inside new goroutine" 19 // defer wg.Done() 20 // ... 21 // }() 22 // wg.Wait() // (may return prematurely before new goroutine starts) 23 // 24 // The correct code calls Add before starting the goroutine: 25 // 26 // // RIGHT 27 // var wg sync.WaitGroup 28 // wg.Add(1) 29 // go func() { 30 // defer wg.Done() 31 // ... 32 // }() 33 // wg.Wait() 34 package waitgroup 35