Go does not expose OS threads directly. You write concurrent code with goroutines and channels, and the runtime scheduler multiplexes goroutines onto a pool of OS threads, by default one per CPU core as set by GOMAXPROCS. For multi-processing, the os/exec package starts separate processes, and os/signal and context coordinate them.
This guide covers goroutines, sync primitives, channels, worker pools, context cancellation and child processes, with corrected examples.
A goroutine is a function running concurrently, started with the go keyword. It starts with a small stack (a few kilobytes) that grows as needed, so programs can run many thousands of them.
Concurrency is not the same as parallelism. Goroutines run concurrently, and the runtime runs them in parallel when several cores are available. main does not wait for goroutines, so never use time.Sleep to wait for them. Use synchronization instead.
The original version of this example forgot to import time, which is a compile error. Since Go 1.25 you can also write wg.Go(func() { ... }), which calls Add and Done for you.
When goroutines share memory, guard it. A data race is undefined behaviour in practice, and the race detector (go run -race, go test -race) finds most of them.
| Tool | Use it for |
|---|---|
sync.Mutex | Exclusive access to shared data |
sync.RWMutex | Many readers, few writers |
sync/atomic | Single counters and flags |
sync.Once | One-time initialization |
sync.Pool | Reusing temporary objects |
sync.Map | Specific cases with mostly-read or disjoint keys |
Go's guideline is to share memory by communicating. A channel carries values between goroutines and synchronizes them.
Only the sender closes a channel. An unbuffered channel makes sender and receiver meet, while a buffered one lets the sender run ahead until the buffer is full.
Starting a goroutine per item is cheap, but unbounded work can still exhaust memory, file descriptors or a downstream service. A worker pool limits concurrency.
The errgroup package (golang.org/x/sync/errgroup) adds error propagation and SetLimit for the same pattern.
Every long-running goroutine needs a way to stop. Pass a context.Context and watch ctx.Done().
Goroutines that never exit are a leak. Always give each one a clear exit path.
Go has no fork. To run work in separate processes, start child processes with os/exec. This helps when you need isolation, another language or a separate memory limit.
Pass arguments separately and never join user input into a shell string. Use pipes (StdinPipe, StdoutPipe) or sockets for communication between processes, and os/signal to handle shutdown signals.
errgroup.SetLimit.context everywhere and cancel on shutdown.go test -race.Hundreds of thousands are common because each starts small. The limit is memory and the work they do, not a thread count.
It sets how many OS threads can run Go code at once. Since Go 1.25 it respects container CPU limits by default.
Use a mutex for protecting a data structure and channels for passing work or results between goroutines.
You cannot kill one from outside. Signal it with a context or a done channel and let it return.
Go replaces explicit threads with goroutines scheduled by the runtime, coordinated through channels, sync and context. For true multi-processing, use os/exec. Bound your concurrency, give every goroutine an exit, and run the race detector.