Go Tutorial: Select & Channel Patterns
Go deeper into concurrency. Timeouts, tickers, fan-in, fan-out, context, rate limiting, graceful shutdown — the patterns every real Go system uses.
Beyond the basics
You know goroutines and channels. Now learn the patterns that production Go systems are actually built on.
These are not theoretical exercises. Every Go service you will ever build will use at least half of what is in this chapter — timeouts, context cancellation, rate limiting, graceful shutdown.
Twelve steps. Advanced, practical, real.
What you'll learn in this Go select & channel patterns tutorial
This interactive Go tutorial has 12 hands-on exercises. Estimated time: 30 minutes.
- Timeout with time.After — Never wait forever. `time.After(d)` returns a channel that receives one value after duration `d`.
- time.Ticker — do something repeatedly — `time.Ticker` fires on a regular interval. Unlike `time.After` which fires once, a ticker fires forever until you stop i…
- Fan-in — merge multiple channels — Fan-in merges multiple channels into one. Any value from any input channel flows out the single output channel.
- Fan-out — distribute work — Fan-out distributes jobs from one channel to multiple worker goroutines.
- Pipeline with cancellation — A pipeline is a series of stages connected by channels. When you want to cancel the whole pipeline early, pass a `done` …
- context.WithTimeout — `context` is the standard Go way to carry deadlines and cancellation signals across function calls and goroutines.
- context.WithCancel — `context.WithCancel` gives you manual control. Call `cancel()` whenever you decide to stop.
- Rate limiting — Rate limiting controls how fast work is processed — to avoid overwhelming a database, an API, or a downstream service.
- Semaphore — limit concurrent access — A semaphore limits how many goroutines can do something at the same time.
- Graceful shutdown — When your server receives a shutdown signal (Ctrl+C, SIGTERM from Kubernetes), you need to:
- Fix a goroutine leak — A goroutine leak is a goroutine that starts but never exits. Over time it consumes memory and eventually crashes your pr…
- Build a real-time event processor — No starter code.
Go Select & Channel Patterns concepts covered
- Beyond the basics