Go supports parallelism by running goroutines on multiple OS threads at once. The runtime scheduler spreads runnable goroutines across GOMAXPROCS threads, which defaults to the number of available CPU cores. To get real speed-ups, you split CPU-bound work into independent chunks, run them in goroutines, bound the concurrency with worker pools, and avoid contention on shared state.
Concurrency is about structuring a program as independent tasks; parallelism is running them at the same time. Go gives you the first, and the scheduler delivers the second when cores are available.
The scheduler maps many goroutines (G) onto OS threads (M) through logical processors (P). The number of Ps equals GOMAXPROCS. Since Go 1.25 the runtime also respects container CPU limits (cgroup quotas on Linux) and adjusts GOMAXPROCS automatically; in older versions you needed a helper like automaxprocs to avoid oversubscribing a limited container.
You rarely need to change GOMAXPROCS. Set the GOMAXPROCS environment variable only when you want to limit a process on purpose.
The simplest pattern is to divide a slice into chunks, process each in its own goroutine, and write results into separate slots so no locking is needed.
This relies on Go 1.22 per-iteration loop variables and the built-in min from Go 1.21. Each goroutine writes only to its own partial[w], but neighbouring slots share a cache line, which can cause false sharing. For hot loops, accumulate in a local variable and write once at the end.
Starting one goroutine per item is fine for a few thousand cheap tasks, but for heavy work or limited resources (database connections, file handles) use a fixed pool reading from a channel.
For error handling and cancellation, errgroup.Group from golang.org/x/sync offers SetLimit(n), which bounds active goroutines without writing the pool by hand.
Locks serialise work, so heavy contention cancels the benefit of more cores. Options, from simplest to most specialised:
| Technique | When it helps |
|---|---|
| Per-goroutine local results, merged at the end | Aggregations such as sums and counts |
sync/atomic types (atomic.Int64) | A single shared counter or flag |
sync.RWMutex | Many readers, rare writers |
| Sharded maps or locks | A hot map accessed by many goroutines |
sync.Pool | Cutting allocation and GC pressure |
Parallelism has overhead, and Amdahl's law limits the gain from the serial part of your program. Always measure.
The -cpu flag runs a benchmark at several GOMAXPROCS values so you can see scaling. Use the race detector for correctness and pprof or go tool trace to find lock contention, idle processors and GC cost. Parallelism does not help I/O-bound work much; there, concurrency with enough goroutines is what matters.
Yes. Since Go 1.5 GOMAXPROCS defaults to the number of cores, so goroutines can run on several cores at once.
Concurrency is composing independent tasks, and parallelism is executing them simultaneously on multiple cores. A concurrent program can run on a single core.
For CPU-bound work, about one per core. For I/O-bound work, as many as the downstream resources allow, bounded by a pool or errgroup.SetLimit.
Common causes are lock contention, false sharing, too little work per goroutine, and allocation pressure. Profile with pprof to see which applies.
Go parallelism rests on goroutines, the multi-core scheduler and GOMAXPROCS. Split CPU-bound work into independent chunks, bound concurrency with worker pools or errgroup, cut contention with local state and atomics, and benchmark with -cpu and pprof before and after every change.