The select statement in Go lets one goroutine wait on several channel operations at once and run whichever case becomes ready first. It is the standard tool for multiplexing channels, adding timeouts, handling cancellation with context, and doing non-blocking sends or receives. If several cases are ready at the same moment, Go picks one of them at random.
A select looks like a switch, but every case is a channel send or receive instead of a boolean expression. The whole statement blocks until one case can proceed, unless it has a default case.
The runtime evaluates all channel expressions once, on entry to the select. Then it follows these rules:
default case, default runs immediately.default, the goroutine blocks until a case is ready.A select with no cases (select {}) blocks forever. A case on a nil channel never proceeds, which is a useful way to switch a case off.
The most common use is reading from more than one source in a single loop.
The loop handles whichever producer sends first, so a slow channel never delays a fast one. Exact ordering varies between runs.
A receive on a channel can block forever if the sender never arrives. Combine it with time.After to bound the wait.
Since Go 1.23, timers that are no longer referenced are garbage collected even if they have not fired, so time.After inside a loop no longer leaks until it expires. On older versions, create a time.NewTimer and call Stop when you loop.
For real programs, prefer context.Context over a hand-made done channel. ctx.Done() returns a channel that closes when the context is cancelled or times out.
Note the inner select on the send. Without it, a worker blocked on out <- ... would ignore cancellation and leak. Always guard blocking sends and receives with a cancellation case.
A default case turns a channel operation into a non-blocking attempt.
Avoid a default inside a tight loop with nothing else in it. It turns the loop into a busy spin that burns a CPU core.
| Mistake | Effect | Fix |
|---|---|---|
break inside a select in a for loop | Leaves only the select, not the loop | Use a labelled break or return |
| Receiving from a closed channel | Returns the zero value immediately, every time | Use v, ok := <-ch and set the channel to nil when closed |
| Blocking send without a cancellation case | Goroutine leak | Add case <-ctx.Done() |
default in a hot loop | 100 percent CPU | Remove default or add a blocking case |
Setting a closed channel variable to nil is the idiomatic way to stop selecting on it while the other cases keep working.
No. A switch evaluates conditions, while a select only works with channel sends and receives and blocks until one is ready.
One of the ready cases is chosen pseudo-randomly. You cannot rely on source order for priority; if you need priority, check the high-priority channel in a first select with a default, then fall back to a combined one.
Use return, or a labelled break such as break loop on the for statement. A plain break only exits the select.
Yes. Each case can be either a receive (<-ch) or a send (ch <- v), and they can be mixed.
Use select to wait on several channels, to add timeouts with time.After or context, and to make operations non-blocking with default. Guard every blocking send and receive with a cancellation case, remember that ready cases are picked at random, and set closed channels to nil to stop selecting on them.