GOMAXPROCS
`GOMAXPROCS` controls how many threads may execute Go code simultaneously; treat it as a measured runtime knob, not a correctness tool.
Canonical guidance
- leave
GOMAXPROCSat the runtime default unless data says otherwise - use it to tune throughput or CPU sharing, not to fix race or scheduling bugs
- re-measure after changing it
Use when
- CPU-bound workloads
- constrained container environments
- runtime investigation under production-like load
Avoid
- pinning a magic value from folklore
- using
GOMAXPROCS=1to hide concurrency bugs - discussing scheduler tuning before measuring
Preferred pattern
- benchmark or trace with the default, then compare a small set of explicit values if needed
Anti-pattern
- changing
GOMAXPROCSin library code
Explanation: This is tempting because local tuning can look effective, but library-level scheduler control surprises callers and rarely generalizes.
Why
GOMAXPROCSis an environment-wide runtime policy knob
Related pages
Sources
- runtime package - Go Team
- Diagnostics - Go Team