Go handles concurrency with goroutines, channels, the select statement and the sync, sync/atomic and context packages. Goroutines are lightweight functions run by the Go scheduler, channels pass values between them safely, and context carries cancellation and deadlines.
The guiding idea is to share memory by communicating: pass data over channels instead of guarding shared variables with locks, and use locks only where shared state is simpler.
Put the go keyword before a function call and it runs concurrently. The Go runtime multiplexes goroutines onto a small number of OS threads (an M:N scheduler), starts them with a few kilobytes of stack that grows as needed, and by default uses as many threads for running Go code as there are CPUs (GOMAXPROCS).
main must wait, because the program exits when main returns even if goroutines are still running. Since Go 1.22 each iteration has its own i, so the closure is safe. In Go 1.25 and later you can also write wg.Go(func() { ... }) to combine Add, go and Done.
A channel is a typed conduit. An unbuffered channel synchronises sender and receiver; a buffered channel (make(chan T, n)) lets senders continue until the buffer is full.
Rules worth remembering:
v, ok := <-ch to detect closure.chan<- T, <-chan T) document and enforce who sends and who receives.select waits on several channel operations and runs the one that is ready, which makes timeouts, cancellation and multiplexing straightforward.
Because results is unbuffered, the worker goroutine in this example would block forever after the timeout. In real code make the channel buffered with capacity 1, or have the worker also watch ctx.Done(), so goroutines never leak.
When shared state is the right design, use the standard primitives.
| Primitive | Use it for |
|---|---|
sync.Mutex, sync.RWMutex | Protecting maps, structs and counters |
sync.WaitGroup | Waiting for a set of goroutines |
sync.Once (and sync.OnceFunc) | One-time initialisation |
sync.Pool | Reusing temporary objects |
sync/atomic | Lock-free counters and flags such as atomic.Int64 |
golang.org/x/sync/errgroup | Goroutine groups with error propagation and limits |
Never copy a struct containing a mutex; go vet reports this.
Pass a context.Context as the first parameter of any function that may block or run for long. It carries cancellation, deadlines and request-scoped values across API boundaries. Always call the cancel function returned by WithCancel or WithTimeout, typically with defer.
errgroup.SetLimit to bound parallel calls.Avoid common mistakes: goroutine leaks from blocked sends or receives, unsynchronised map access (which can crash the program), and starting unbounded goroutines per request. Run go test -race ./... regularly, because the race detector finds data races that code review misses.
No. A goroutine is managed by the Go runtime, is much cheaper than an OS thread and can be created in large numbers. The runtime schedules many goroutines onto few threads.
No. Concurrency is structuring work as independent tasks; parallelism is running them simultaneously. Go programs run in parallel automatically when multiple CPUs are available.
Use channels to transfer ownership or coordinate stages, and mutexes to protect simple shared state. Either is idiomatic when it fits the problem.
Give every goroutine a way to exit, usually by selecting on ctx.Done() or by closing the channel it ranges over, and make sure senders are never left blocked.
Go concurrency is built from goroutines, channels, select, the sync and atomic packages and context. Start goroutines freely but bound them, communicate through channels where it clarifies ownership, protect shared state with mutexes where it is simpler, cancel through context, and verify with the race detector.