Go concurrency is built on goroutines (lightweight functions scheduled by the Go runtime), channels (typed pipes for passing values between goroutines) and the sync, context and select tools that coordinate them. Go's guiding idea is to share memory by communicating, so goroutines usually hand data to each other over channels rather than locking shared variables.
Concurrency is not the same as parallelism. Concurrency is structuring a program as independent tasks; parallelism is running them at the same moment on several CPU cores. The Go scheduler gives you both, using GOMAXPROCS (which defaults to the number of available CPUs) to decide how many operating system threads run Go code at once.
A goroutine is a function running concurrently with the code that started it. Start one with the go keyword. Goroutines begin with a small stack that grows as needed, so programs can run hundreds of thousands of them.
main does not wait for goroutines on its own: when main returns, the program exits. A sync.WaitGroup counts running goroutines and blocks wg.Wait() until each has called Done. Since Go 1.22 every loop iteration gets its own i, so the closure above is safe without copying the variable. Go 1.25 also added wg.Go(func() { ... }), which combines Add, the go statement and Done.
A channel carries values of one type between goroutines and synchronises them while doing so.
Key rules:
make(chan int, 10)) lets the sender continue until the buffer is full.ok == false.chan<- int (send only) and <-chan int (receive only) in function signatures to document intent.select.select waits on multiple channel operations and runs the first that is ready. With default it never blocks, and with time.After or a context it can time out.
If several cases are ready, Go picks one at random, so do not rely on ordering.
When goroutines share state, protect it.
| Tool | Use it for |
|---|---|
sync.Mutex, sync.RWMutex | Guarding shared data with exclusive or read-mostly access |
sync.WaitGroup | Waiting for a group of goroutines to finish |
sync.Once | Running initialisation exactly once |
sync/atomic | Lock-free counters and flags |
sync.Map, sync.Pool | Special-case concurrent map and object reuse |
errgroup.Group (golang.org/x/sync) | Running tasks and collecting the first error |
Run tests with go test -race to find data races. A data race, two goroutines touching the same variable with at least one write and no synchronisation, is undefined-behaviour territory and a common source of bugs.
Every goroutine you start needs a way to stop. The standard answer is context.Context, passed as the first parameter.
Forgetting to stop a goroutine causes a goroutine leak: it stays blocked forever, holding memory. Always ask who closes the channel or cancels the context.
Choose a channel when you pass ownership of data or coordinate steps, and a mutex when you simply protect a small piece of shared state. Both are idiomatic Go.
A goroutine is managed by the Go runtime, not the operating system. It starts with a tiny stack and is multiplexed onto a smaller number of OS threads, so it is far cheaper to create and switch.
Yes, when more than one CPU is available. The runtime schedules goroutines across threads up to GOMAXPROCS, so independent goroutines can execute at the same time.
Use a mutex to protect shared state such as a cache or counter. Use channels to transfer data or signal events between goroutines.
Give each goroutine a clear exit path: a context cancellation, a closed channel, or a completed task. Test with go test -race and monitor runtime.NumGoroutine().
Go concurrency rests on goroutines started with go, channels for communication, select for waiting on several events, sync primitives for shared state and context for cancellation. Start goroutines only when you know how they end, wait for them with a WaitGroup or errgroup, and run the race detector regularly.