Go supports concurrency with goroutines, channels, select, the sync package and the context package, all built into the language and standard library. The runtime scheduler multiplexes many goroutines onto a small number of OS threads and uses all available CPU cores, so the same code is concurrent and parallel. That combination is why Go is common for servers, proxies and data pipelines that must handle many simultaneous tasks.
Concurrency is about structuring a program to handle many tasks; parallelism is running tasks at the same instant on multiple cores. Go gives you the first with goroutines and the second automatically through its scheduler.
A goroutine is a function running concurrently with other goroutines. You start one with the go keyword. Goroutines begin with a small stack (a few kilobytes) that grows as needed, so a program can run hundreds of thousands of them, far more than OS threads allow.
sync.WaitGroup waits for all goroutines to finish. Without it, main would return and end the program before the goroutines ran. Since Go 1.22 each loop iteration has its own i, so the closure captures the right value. Go 1.25 also added wg.Go(func()) as a shorthand for the Add, go and Done pattern.
Channels pass values between goroutines and synchronise them. The Go proverb sums up the approach: do not communicate by sharing memory; share memory by communicating.
An unbuffered channel blocks the sender until a receiver is ready; a buffered channel (make(chan int, 100)) lets senders continue until the buffer is full. The select statement waits on several channel operations at once and is how you add timeouts and cancellation:
Channels are not the only tool. For protecting shared state, sync.Mutex, sync.RWMutex and sync/atomic are often simpler and faster than a channel.
The scheduler maps goroutines (G) onto OS threads (M) through logical processors (P). The number of Ps is GOMAXPROCS, which defaults to the number of CPU cores available. You rarely need to set it. Since Go 1.25, the default also respects CPU limits set through Linux cgroups, which makes containerised services behave better than before.
Calling runtime.GOMAXPROCS(0) reads the current value without changing it. Setting a different number is only useful for specific tuning. Blocking system calls are handled by the runtime, which moves other goroutines to other threads, so one blocked goroutine does not stall the rest.
Unbounded goroutines can exhaust memory or overload a downstream database. Limit them with a pool:
The producer closes jobs when done, workers exit their loops, and a final goroutine closes results after all workers finish so the range loop ends.
Every request-scoped operation should accept a context.Context, so timeouts and cancellation propagate through all goroutines it started. The golang.org/x/sync/errgroup package combines a group of goroutines with the first-error cancellation, and supports SetLimit to bound concurrency.
| Problem | Cause | How to catch or fix it |
|---|---|---|
| Data race | Two goroutines access a variable and one writes | Run tests with go test -race; protect with a mutex or channel |
| Goroutine leak | A goroutine blocks forever on a channel | Use context cancellation and select |
| Deadlock | All goroutines wait on each other | The runtime reports all goroutines are asleep; review lock order |
| Unbounded fan-out | A goroutine per incoming item | Use worker pools or errgroup.SetLimit |
Concurrency is composing independent tasks; parallelism is running them at the same time. Goroutines express concurrency, and the scheduler provides parallelism when multiple cores are available.
Hundreds of thousands are practical, limited mainly by memory and by what the goroutines do. Each starts with a small stack.
No. Use mutexes or atomics for simple shared state and channels when you pass ownership of data or coordinate stages.
Build or test with the -race flag. The race detector reports conflicting accesses at runtime.
Go's goroutines, channels, select, sync and context make concurrent code short and scalable, while the runtime scheduler spreads work across cores. For large systems, bound concurrency with worker pools, pass context everywhere, and run the race detector in CI.