The select Statement
Multiplexing channels with select
The select Statement is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is select?
The select statement lets a goroutine wait on multiple channel operations simultaneously. It picks the first case that is ready to proceed, similar to a switch but for channels.
Basic Syntax
Each case in a select is a channel send or receive:
select {
case msg := <-ch1:
fmt.Println("ch1:", msg)
case msg := <-ch2:
fmt.Println("ch2:", msg)
}Non-blocking with default
Adding a default case makes the select non-blocking — if no channel is ready, it falls through to default immediately.
select {
case v := <-ch:
fmt.Println(v)
default:
fmt.Println("no value ready")
}Random selection
When multiple cases are ready at the same time, Go picks one at random — every ready case has an equal chance of being selected. This prevents starvation.
Sending in select
Cases can also be channel sends. A send case is ready when the channel has buffer space (or a receiver is waiting).
select {
case ch1 <- val1:
fmt.Println("sent to ch1")
case ch2 <- val2:
fmt.Println("sent to ch2")
}Empty select blocks forever
A bare select{} with no cases blocks the current goroutine forever. This is a common idiom to keep the main goroutine alive while other goroutines run.
func main() {
go serve()
select {} // block forever
}Looping with select
Wrap a select inside a for loop to continuously multiplex channels. Add a done channel to break out cleanly.
for {
select {
case v := <-data:
process(v)
case <-done:
return
}
}Nil channels
A nil channel blocks forever in a select case, effectively disabling that case. You can use this to dynamically enable or disable cases at runtime.
var ch chan int // nil
select {
case v := <-ch: // never selected
_ = v
case v := <-other:
fmt.Println(v)
}Select with closed channels
A receive from a closed channel always succeeds immediately, returning the zero value. Guard with the two-value form to detect closure.
select {
case v, ok := <-ch:
if !ok { return } // channel closed
use(v)
}Priority patterns
Go select does not support priorities natively. To prioritize one channel, nest selects or use a loop that checks the high-priority channel first with a default.
for {
select {
case v := <-highPriority:
handle(v)
default:
select {
case v := <-lowPriority:
handle(v)
}
}
}Recap so far
select waits on multiple channels, picks a random ready case, supports default for non-blocking, and nil channels disable cases. Common pattern: for+select+done.
Quick Check
What happens when multiple cases in a select are ready at the same time?
Recap: The select Statement
Key points:
- select blocks until one channel case is ready
- Multiple ready cases → random selection
- default makes it non-blocking
- nil channel disables its case
- for+select+done is the standard multiplexing loop
Frequently asked questions
Is the “The select Statement” lesson free?
Yes — the full text of “The select Statement” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “The select Statement”?
Multiplexing channels with select You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The select Statement” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The select Statement
- Timeouts and Tickers
- Fan-in and Fan-out Patterns
- Worker Pool Pattern