Go Concurrency Explained: Goroutines and Channels
A practitioner's tour of goroutines, channels, select, the sync package, context, and the pitfalls that leak or deadlock real Go services.
Key takeaways
- Goroutines are cheap functions the Go runtime schedules onto a few OS threads, and channels pass data between them safely.
- Use channels to hand off ownership, sync.Mutex for small shared state, and sync.WaitGroup to wait for a group.
- Bound concurrency with worker pools, pass a context for cancellation, and run go test -race in CI.
On this page
Go concurrency rests on goroutines, lightweight functions started with go and multiplexed by the runtime onto a few OS threads, and channels, typed pipes that pass values between them. Use select to wait on several channels, the sync package for locks and joins, and context for cancellation. Bound everything with a worker pool.
Verified against
Go 1.27 (go1.27.0, released 2026-08-19), against the Go language specification, the go.dev release notes for Go 1.22 and 1.25, the sync package docs and the race detector article. Checked October 2026.
Concurrency is the part of Go people fall in love with and the part that later keeps them up at night. The primitives are small and the syntax is friendly, which makes it easy to write something that works on your laptop and leaks goroutines in production.
Goroutines: cheap by design
A goroutine is a function running independently of the one that started it. You launch one with the go keyword and Go handles the rest. What makes them different from OS threads is cost. A goroutine starts with a tiny stack (a few kilobytes) that grows and shrinks on demand, and the Go runtime multiplexes many of them onto a small pool of OS threads. That scheduling happens in user space, so switching between goroutines skips the kernel round trip a thread context switch pays.
The practical result: a web server handling ten thousand concurrent requests with ten thousand goroutines is unremarkable. You would never do that with raw OS threads.
The catch is that a bare goroutine gives you no way to know when it finished or to collect what it produced. go doWork() fires and forgets. To coordinate, you need a way to communicate.
Channels: communicate to share
Channels are typed conduits. You send values in one end and receive them out the other, and the channel handles the synchronization for you. The Go proverb is worth memorizing: don't communicate by sharing memory; share memory by communicating. Instead of guarding a shared variable with a lock, you pass ownership of the data through a channel, so only one goroutine touches it at a time.
Here is the pattern that replaces fire-and-forget. A worker computes a result and sends it back:
func main() {
results := make(chan int)
for i := 1; i <= 3; i++ {
go func(n int) {
results <- n * n // send the square back
}(i)
}
// Receive exactly three results.
for i := 0; i < 3; i++ {
fmt.Println(<-results)
}
}
The loop variable is passed as an argument. Capturing it directly was a classic bug for years; since Go 1.22, each iteration of a for loop creates new variables, so modules declaring go 1.22 or later no longer share one across goroutines.
Buffered vs unbuffered
The channel above is unbuffered: a send blocks until another goroutine is ready to receive, and vice versa. That makes an unbuffered channel a synchronization point as much as a data pipe. The sender and receiver rendezvous.
A buffered channel, make(chan int, 10), holds up to its capacity without a receiver waiting. Sends only block when the buffer is full; receives only block when it is empty. Buffers smooth bursts and decouple producer and consumer speeds, but they are not a license to ignore backpressure. If your producer is permanently faster than your consumer, a buffer just delays the moment things fall over.
Rule of thumb: start unbuffered. Add a buffer only when you have measured a reason, and size it to a number you can defend.
Select: waiting on many things
select lets one goroutine wait on multiple channel operations at once, proceeding with whichever is ready first. It is how you build timeouts, cancellation, and fan-in.
select {
case msg := <-work:
process(msg)
case <-time.After(2 * time.Second):
return errors.New("timed out waiting for work")
case <-ctx.Done():
return ctx.Err()
}
If several cases are ready, the spec says select picks one by uniform pseudo-random selection, which keeps any single channel from starving the others. A default case makes the whole thing non-blocking.
When to skip channels: the sync package
Channels are the right tool for handing data between goroutines. They are the wrong tool for the simplest jobs. If you just need to wait for a batch of goroutines to finish, use a sync.WaitGroup. If you need to protect a counter or a map, a sync.Mutex is clearer and faster than routing every access through a channel.
var wg sync.WaitGroup
for _, url := range urls {
wg.Add(1)
go func(u string) {
defer wg.Done()
fetch(u)
}(url)
}
wg.Wait() // blocks until all Done calls land
Since Go 1.25, wg.Go(func() { fetch(u) }) does the Add(1) and defer wg.Done() for you, which removes the most common WaitGroup mistake: calling Add inside the goroutine instead of before it.
The guideline we use: channels for transferring ownership of data or signaling events, mutexes for protecting a small piece of shared state, WaitGroups for "wait until this group is done." Reaching for a channel to guard a single integer is a smell.
Context: cancellation and deadlines
Long-running goroutines need a way to be told to stop, whether a request was cancelled or a deadline passed. That is what context.Context is for. You pass a context down through your call tree, and any goroutine can watch ctx.Done() and bail out. context.WithTimeout and context.WithCancel give you a context plus a cancel function you should always defer. Every network call and blocking operation in a server should accept a context, otherwise cancellation stops at your door.
The worker pool
Unbounded goroutine spawning is one of the most common ways to sink a Go service. A request comes in, you launch a goroutine per item, traffic spikes, and suddenly you have a million goroutines fighting over a database with a connection pool of twenty. The fix is a worker pool: a fixed number of goroutines pulling from a shared jobs channel.
func pool(jobs <-chan int, results chan<- int, workers int) {
var wg sync.WaitGroup
for w := 0; w < workers; w++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := range jobs { // exits when jobs is closed
results <- j * 2
}
}()
}
wg.Wait()
close(results)
}
The producer closes jobs when it has queued everything. Ranging over a channel ends cleanly on close, so each worker returns, the WaitGroup drains, and results closes. This bounds your concurrency to a number you chose on purpose.
The pitfalls
Goroutine leaks: a goroutine blocked forever on a channel that will never receive never gets collected. Every launched goroutine needs a guaranteed exit path, usually a closed channel or a cancelled context.
Deadlocks: all goroutines blocked waiting on each other. The runtime detects the total case and panics, but partial deadlocks that hang a subset of your program are on you to find.
Closed-channel panics: sending on a closed channel panics, and closing a channel twice panics. The sender closes, never the receiver, and only one goroutine should own the close.
Data races: two goroutines touching the same memory without synchronization is a bug that may or may not show up in testing. Build and test with the race detector; it only reports races on code paths that actually execute, so run it against realistic tests:
go test -race ./...
go run -race ./cmd/server
Questions people ask
What is the difference between a goroutine and a thread?
A goroutine is a function scheduled by the Go runtime, not the operating system. It starts with a stack of a few kilobytes that grows on demand, and the runtime multiplexes many goroutines onto a small pool of OS threads, switching between them in user space. That makes thousands of concurrent goroutines routine, where thousands of OS threads would exhaust memory and scheduler time.
When should I use a buffered channel in Go?
Start unbuffered, because an unbuffered send waits for a receiver and gives you a natural synchronization point. Add a buffer when you have measured a burst the consumer can absorb later, or when a known number of goroutines each send exactly one result and should not block. A buffer never fixes a producer that is permanently faster than its consumer; it only delays the backlog.
How do I find and prevent goroutine leaks?
Give every goroutine a guaranteed exit: a channel the sender closes, or a context.Context whose Done() it watches in a select. Leaks show up as a goroutine count that only grows, so export runtime.NumGoroutine() as a metric and grab a goroutine profile from net/http/pprof when it climbs. The stack traces point at the channel operation each leaked goroutine is stuck on.
The call we'd make
For most services, keep it boring. Bound your concurrency with worker pools, thread a context through every goroutine, close channels from the sender only, and let sync.WaitGroup handle the joins. Run the race detector in CI and treat any finding as a bug, not a warning.
If you are weighing Go against other options for a concurrent backend, our pillar on Rust vs Go covers the tradeoffs in depth, and Go vs Python performance looks at where Go's scheduler pulls ahead on real workloads.
Sources
Cite this article
Kiril Urbonas (2026). Go Concurrency Explained: Goroutines and Channels. DevOpsNess. Updated 2026-11-15. https://www.devopsness.com/blog/go-concurrency-explained
[Go Concurrency Explained: Goroutines and Channels](https://www.devopsness.com/blog/go-concurrency-explained) (Kiril Urbonas, DevOpsNess, updated 2026-11-15)
More from DevOps
Explore more articles in this category
Kubernetes Pod Disruption Budgets: Surviving Node Drains Without an Outage
Node upgrades, autoscaler scale-downs, and spot reclaims all drain nodes. Without PDBs they can take all your replicas at once. The budgets, probes, and graceful-shutdown handling that keep voluntary disruptions invisible to users.
GitHub's September 13 Outage: One Cleanup Job, Many Services
A background cleanup job saturated a shared database behind GitHub, and the safeguard pacing it watched only replica lag. Throttle on more than one signal.
Getting Started with Istio: A Hands-On Tutorial (2026)
A practical walkthrough to install Istio, turn on automatic mTLS, and run a canary traffic split on Kubernetes.
You might have missed
Evergreen posts worth revisiting.