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.