Select Statement Basics
Fast recall
Quick revision
Core answer
Whether you understand Select Statement Basics beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
select composes channel operations into one wait:
select {
case msg := <-ch1:
fmt.Println("from ch1:", msg)
case msg := <-ch2:
fmt.Println("from ch2:", msg)
case <-time.After(50 * time.Millisecond):
fmt.Println("timeout")
}Executed: ch2 became ready first (10ms vs 30ms) — select chose it. When MULTIPLE are ready, the choice is RANDOM (fairness — no case starves).
The shape: cases are channel ops — receive (<-ch) or send (ch <- v). select blocks until at least one case can proceed, runs exactly ONE case, then continues.
The three variants:
- default: non-blocking probe (executed: 'nothing ready — didn't block')
- time.After: the timeout pattern (executed: 'timeout fired first')
- for { select { } }: the worker's main loop
The use: a goroutine serving multiple channels — work AND cancellation AND results — is a for/select. Sequential receives would block on the first empty channel; select waits on ALL simultaneously.
Select Statement Basics
Direct Answer
select is switch for channels: each case is a channel operation. Blocks until one case is ready, picks RANDOMLY among ready ones. With default: never blocks. With time.After: timeout.
Deep Dive & Architecture
The mechanics: select locks ALL its channels (fixed internal order to prevent deadlock between selects), evaluates every case for readiness, picks among ready cases RANDOMLY — the fairness mechanism. Without it, a busy channel 1 would starve channel 2 (first-listed always winning).
The deadlock-free locking: multiple channels locked together atomically — two selects over overlapping channels can't partially acquire and stall.
The default's dual semantics: default with ALL cases not-ready → run default. default with a ready case → the ready case still runs. The common bug: expecting default to interrupt a long-running case — it only answers 'was anything ready RIGHT NOW.'
select{} with no cases: blocks FOREVER — the deliberate permanent-park.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Select Statement Basics in a real project?
- How would you compare Select Statement Basics with an alternative?
- What is a common mistake when using Select Statement Basics?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.