Goroutines and channels let a Go program do many things at once and coordinate them safely. Use goroutines to run independent work concurrently, such as handling requests or fetching several URLs, and use channels to collect results, signal completion and pass work between stages without sharing variables.
This article focuses on how to apply them in real programs: fan-out and fan-in, pipelines, error handling with errgroup, and the mistakes that cause leaks and deadlocks.
Reach for a goroutine when work is independent and either waits on I/O (HTTP calls, database queries, file reads) or can be spread across CPU cores. Typical cases are:
net/http server already runs every request in its own goroutine.Do not start a goroutine for trivial work. Each goroutine needs an owner who knows when it ends.
The program waits for all three tasks before exiting. Output order varies between runs. From Go 1.22, each loop iteration gets its own i, so the closure captures the right value.
A channel is a typed pipe. Unbuffered channels hand off values directly and synchronize sender and receiver; buffered channels hold a fixed number of values before the sender blocks.
for range 3 (ranging over an integer) needs Go 1.22 or later. Closing a channel signals "no more values"; only the sender should close it, and receivers detect it with v, ok := <-ch or by ranging over the channel.
Fan-out starts several workers reading from the same input channel. Fan-in merges their outputs into one channel.
Each stage owns the channel it creates and closes it when finished, which lets range loops end cleanly. This staged design is called a pipeline.
Plain goroutines have no way to return an error. The golang.org/x/sync/errgroup package runs a group of goroutines, returns the first error and can cancel the others through a shared context:
Use g.SetLimit(n) to cap how many goroutines run at once.
| Mistake | Result | Fix |
|---|---|---|
| Sleeping instead of waiting | Flaky or truncated output | Use sync.WaitGroup or a channel |
| Sending with no receiver | Deadlock: "all goroutines are asleep" | Match every send with a receive, or buffer |
| Closing from the receiver or twice | Panic | Let the single sender close |
| Goroutine blocked forever | Leak that grows memory | Select on ctx.Done() |
| Shared variable without a lock | Data race | Use a channel, sync.Mutex or sync/atomic; test with -race |
Hundreds of thousands are feasible because each starts with a small stack, though real limits depend on memory and what the goroutines do. Bound concurrency for work that touches external systems.
Both. They are concurrent by design and run in parallel across CPU cores up to GOMAXPROCS, which defaults to the number of available cores.
Only when you know the maximum number of items or want to smooth short bursts. A buffer does not remove the need for a consumer, and an oversized buffer can hide a bottleneck.
Send it on a channel, write it into a slot of a pre-sized slice that only that goroutine touches, or use errgroup when errors matter.
Goroutines provide cheap concurrency and channels provide safe communication. Use WaitGroup or errgroup to wait, build pipelines with fan-out and fan-in for parallel stages, close channels only from the sender, and pass a context so goroutines can be cancelled. Run tests with the race detector to catch unsafe sharing early.