A switch statement picks one branch by comparing a value, a boolean condition or a type, and it is plain control flow. A select statement waits on several channel send and receive operations and runs the branch whose operation can proceed. If more than one is ready, select chooses one at random, and if none is ready it blocks until one is, or runs default when present.
Both look similar because each has case clauses, but they solve different problems. Use switch for decisions inside one goroutine, and select for coordinating goroutines, timeouts and cancellation.
Go's switch evaluates cases from top to bottom and runs the first match. There is no automatic fall-through, so you do not write break at the end of each case. Cases can hold several values, and a switch without an expression is a clean replacement for an if/else if chain.
A switch can include an initialisation statement, such as switch x := compute(); x { ... }. The keyword fallthrough forces execution to continue into the next case body, and it is rarely needed.
A type switch compares the dynamic type of an interface value. It is the idiomatic way to branch on what a value actually is.
Inside each case, x has the matching concrete type. The original shorthand interface{} and any mean the same thing since Go 1.18.
Every case in a select must be a channel operation, either a receive (<-ch), a receive with assignment (v := <-ch, v, ok := <-ch) or a send (ch <- v). The statement blocks until one case can proceed, then runs that case only.
The loop for range 2 needs Go 1.22 or later. On older versions write for i := 0; i < 2; i++. Each iteration blocks until a message arrives, so the output follows the order in which the channels become ready.
default case runs immediately when no channel is ready, which makes the operation non-blocking.select with no cases (select {}) blocks forever.nil to disable its case.break inside a select leaves only the select, not an enclosing for loop. Use a labelled break or return to leave the loop.The most common real-world uses of select are timeouts and context cancellation.
The worker handles a job, then the context deadline fires and the ctx.Done() case ends the loop. Calling time.After inside a loop creates a new timer on every iteration. For long-running loops, reuse a time.Timer or rely on the context deadline.
Add default to try an operation without waiting.
Avoid putting default in a loop around select with nothing else, because it turns the loop into a busy wait that burns CPU.
| Feature | select | switch |
|---|---|---|
| Purpose | Wait on channel operations | Branch on a value, condition or type |
| Case contents | Channel sends and receives only | Any expression, or types in a type switch |
| Matching | First ready operation, random among ready ones | First matching case, top to bottom |
| Blocking | Blocks until a case is ready | Never blocks by itself |
fallthrough | Not allowed | Allowed |
default | Makes it non-blocking | Runs when nothing matches |
| Typical use | Timeouts, cancellation, fan-in | Parsing, state handling, type checks |
No. Every case in a select has to be a channel send or receive. To branch on values or conditions, use switch or if.
The runtime chooses one of the ready cases at random. This is deliberate and prevents starvation of any one channel.
No. Go runs only the matching case and then leaves the switch. Use the explicit fallthrough keyword if you really want the next body to run too.
Use return, or a labelled break such as break loop. A plain break only exits the select.
Use switch to choose a branch from values, conditions or types, and select to wait on channel communication, including timeouts via time.After or context. Remember that select picks randomly among ready cases, blocks without default, and that break exits only the select. Together they cover most control flow in concurrent Go programs.