// library
Severity
Bucket
Every goroutine you start should be managed. Avoid launching "fire-and-forget" goroutines that run indefinitely or without any synchronization. Provide a way to stop goroutines (using a cancel context or done channel) and/or track them (e.g., with sync.WaitGroup) to prevent leaks.
When using a WaitGroup to synchronize goroutines, call Add() before launching each goroutine and ensure each goroutine calls Done() exactly once. Finally, call Wait() to block until all goroutines have called Done(). Misordering Add/Done or mismatched calls will cause issues.
Do not use global (package-level) variables for mutable state whenever possible. Instead, encapsulate state in structs or pass it as parameters. If you must use a global variable (for configuration or caching), protect it with mutexes if concurrent access is possible and document its usage.
By default, use unbuffered channels or a buffer size of one. Only use larger buffers when necessary and carefully considered. Unbuffered channels (size 0) or a size of 1 are the most common; a large buffer can hide synchronization issues or lead to memory bloat if misused.
Functions that perform external calls or long operations should accept a `context.Context` parameter and respect its cancellation. Use context timeouts or cancellation signals to stop work that is no longer needed.
17 rules