Go runs work concurrently with goroutines, lightweight functions scheduled by the Go runtime, and coordinates them with channels, sync primitives and context. Concurrency is about structuring a program as independent tasks, while parallelism means those tasks actually run at the same time on multiple CPU cores, which Go does automatically when GOMAXPROCS allows it.
This guide covers the techniques you use in real programs: goroutines, channels, sync.WaitGroup, mutexes, errgroup, worker pools, context cancellation and the race detector.
A goroutine starts with the go keyword. It begins with a small stack that grows as needed, so a program can run many thousands of them. The runtime multiplexes goroutines onto operating system threads, and since Go 1.5 GOMAXPROCS defaults to the number of available CPUs (container CPU limits are respected from Go 1.25).
The output order varies between runs because goroutines are scheduled independently. Do not use time.Sleep to wait for goroutines; use a sync.WaitGroup so main waits exactly as long as needed. Since Go 1.22 each loop iteration has its own i, so the closure above is safe. In Go 1.25 and later you can also write wg.Go(func() { ... }), which combines Add, the goroutine start and Done.
Channels pass values between goroutines and synchronise them. Go's guidance is to share memory by communicating instead of communicating by sharing memory.
Key rules:
make(chan int, 10)) blocks only when full.<-chan int, chan<- int) in signatures to document intent.select picks a ready case at random, and a default case makes it non-blocking.
When goroutines share data, guard it. Use sync.Mutex or sync.RWMutex for plain state, and sync/atomic for single counters.
for range 1000 needs Go 1.22 or later. Also useful are sync.Once for one-time initialisation, sync.OnceValue for lazily computed values, and atomic.Int64 for lock-free counters.
Starting an unbounded number of goroutines for large inputs exhausts memory or downstream services. A worker pool limits concurrency:
The workers read from jobs until it is closed, and a separate goroutine closes results after all workers finish so the final range ends.
Long-running goroutines should stop when their caller gives up. Pass a context.Context and watch ctx.Done(). The golang.org/x/sync/errgroup package runs a group of goroutines, returns the first error and cancels the rest.
This is the standard pattern for fan-out work such as calling several services in parallel with one deadline.
Concurrency bugs are timing dependent, so use the tools:
go test -race ./... and go run -race detect data races at run time.go vet catches copied mutexes and misuse of WaitGroup.Concurrency is structuring work as independent tasks; parallelism is running them simultaneously. Goroutines give you concurrency, and the runtime provides parallelism when several CPU cores are available.
Hundreds of thousands are practical because each starts with a small stack, but the real limit is memory and the resources they touch, so bound work with a pool or errgroup.SetLimit.
Use channels to pass ownership or coordinate stages, and mutexes to protect simple shared state such as a cache or counter. Choose whichever is simpler to read.
When main returns the program ends, regardless of other goroutines. Wait with a sync.WaitGroup, a channel or errgroup.
Use goroutines to start concurrent work, channels and select to communicate, sync and atomic to guard shared state, worker pools or errgroup to bound concurrency, and context to cancel. Test with go test -race and make sure every goroutine has a way to exit.