16.6 Combining select with for: The Event Loop Pattern

Right, so you’ve met select and you’ve met for. Individually, they’re useful. Together, they form the bedrock of most concurrent Go programs you’ll ever write. This is the pattern you use when you need to manage multiple communication operations—waiting on several channels at once without knowing which will be ready first. It’s the event loop for the pragmatic gopher. The basic idea is devilishly simple: you wrap a select statement inside an infinite for loop. This lets you continuously listen for events—messages coming in, signals to shut down, timers expiring—and handle them as they arrive, all within a single goroutine.

16.5 Done Channel Pattern: Cancellation with select

Now, let’s talk about one of the most genuinely useful patterns you’ll employ with select: using a done channel for clean and responsive cancellation. This is the pattern that saves you from the dreaded “my goroutine is stuck forever” scenario, and it’s so idiomatic it might as well be Go’s official way of saying “stop what you’re doing.” The core idea is simple yet brilliant. You pass a <-chan struct{} (typically named doneCtx or ctx.Done()) into a goroutine. This channel will never receive any meaningful data; its sole purpose is to be closed when it’s time to shut things down. The goroutine then uses a select statement to simultaneously listen for its normal work and this cancellation signal. When the done channel is closed, the case for it is immediately selected (because receiving from a closed channel always returns the zero value), and the goroutine can bail out. It’s a fire alarm, not a mail slot.

16.4 Non-Blocking Channel Operations with default

Right, so you’ve got your select block primed and ready to listen on a bunch of channels. But what happens when none of them are ready? By default, select will block, sitting there patiently (or, let’s be honest, completely uselessly) until at least one of its cases can proceed. That’s often what you want, but sometimes you need to do something else while you’re waiting. You need a way to peek at the channels and say, “Nothing? Alright, I’ll go do something useful and check back in a bit.” That’s where the default case comes in.

16.3 Implementing Timeouts with time.After

Now, let’s talk about saving your select statements from hanging around forever like a bad party guest. You’ve got a goroutine waiting on a channel, and you’re thinking, “What if that signal never comes?” Enter time.After. This isn’t just a function; it’s your get-out-of-jail-free card for channel operations. The time.After function returns a channel (<-chan Time). You don’t get to send on it; you only get to receive from it. After the duration you specify, the runtime will send the current time on that channel. Exactly once. It’s a one-shot deal. The real magic happens when you drop this channel into a select statement alongside your other cases. It gives the entire operation a hard deadline.

16.2 Randomized Selection When Multiple Cases Are Ready

Right, so you’ve got a select statement with multiple cases that are all ready to fire at the same time. What happens? Chaos? Anarchy? A coin flip in the heart of the Go runtime? Precisely. It’s a coin flip. This is one of those beautiful, pragmatic, and occasionally infuriating design choices Go makes. The language spec doesn’t dictate a strict, predictable order. Instead, when multiple cases in a select are ready to proceed—meaning multiple channels have data to receive or are ready to send—one is chosen pseudo-randomly. I say “pseudo-randomly” because it’s not truly random (it’s deterministic from the runtime’s perspective), but from your code’s perspective, it’s effectively random. You can’t predict it.

16.1 select: Waiting on Multiple Channel Operations

Right, so you’ve got goroutines firing off left and right, channels shuttling data all over the place. It’s beautiful chaos. But what happens when you need to listen to more than one channel at a time? You can’t just sit on a single <-ch receive operation; that’s like trying to listen to two conversations by putting your ear to one person’s mouth. You need a better tool. You need select.

— joke —

...