The Go select statement lets one goroutine wait on several channels at once and react to the first one ready. In this post, you will add timeouts, make non-blocking checks and stop a loop cleanly. Also, each idea comes with a short example that you can run yourself.
You need channels for this lesson, so first read channels in Go with examples. You also used select with ctx.Done() in Go context for beginners, and now you will learn it properly.
What Is the Go Select Statement?
A select looks like a switch, but every case is a channel operation. It blocks until one case can proceed, then runs that case only. If several are ready at once, Go picks one at random, so no channel gets ignored forever.
For example, think of a waiter watching three tables. Of course, the waiter does not stand at one table. Instead, the waiter serves whichever table raises a hand first.
fast := make(chan string)
slow := make(chan string)
go func() {
time.Sleep(50 * time.Millisecond)
fast <- "fast result"
}()
go func() {
time.Sleep(200 * time.Millisecond)
slow <- "slow result"
}()
for i := 0; i < 2; i++ {
select {
case msg := <-fast:
fmt.Println(msg)
case msg := <-slow:
fmt.Println(msg)
}
}
// fast result
// slow result
First of all, the loop runs select twice. On the first pass, fast is ready. Then, on the second pass, only slow is left. In fact, the order of the cases does not matter at all. Instead, only the timing of the channels decides which case runs.
Adding a Timeout to a Go Select Statement
result := make(chan string)
go func() {
time.Sleep(2 * time.Second)
result <- "done"
}()
select {
case r := <-result:
fmt.Println(r)
case <-time.After(100 * time.Millisecond):
fmt.Println("timeout after 100ms")
}
// timeout after 100ms
In short, time.After returns a channel that receives a value after the given time. So the Go select statement can race real work against a clock. For instance, the diagram below shows that race.

The select waits on both channels; the 100 ms timer fires first, so the timeout case runs and the slow result is ignored.
In a real server, prefer a context with a deadline. However, time.After is perfect for small scripts and tests.
Non-Blocking Checks with default
messages := make(chan string)
select {
case m := <-messages:
fmt.Println(m)
default:
fmt.Println("no message ready")
}
// no message ready
A default case runs when no channel is ready. As a result, the select never blocks. For example, you can try to send a log line and simply drop it if the buffer is full.
The for-select Loop
ticker := time.NewTicker(30 * time.Millisecond)
defer ticker.Stop()
done := make(chan struct{})
go func() {
time.Sleep(100 * time.Millisecond)
close(done)
}()
ticks := 0
loop:
for {
select {
case <-ticker.C:
ticks++
case <-done:
break loop
}
}
fmt.Println("ticks:", ticks) // ticks: 3 (may vary slightly)
Indeed, this is the most common shape of a Go select statement in real code. The loop keeps handling events until a done channel closes. Also, note the label loop, because a plain break would only leave the select, not the for.
Turning Off a Case with nil
In general, a receive from a nil channel blocks forever. So setting a channel variable to nil switches off its case. This trick helps when you merge two channels and one closes before the other.
Go Select Statement Rules at a Glance
| Situation | What select does |
|---|---|
| One case ready | Runs that case |
| Several cases ready | Picks one at random |
| No case ready, no default | Blocks and waits |
| No case ready, with default | Runs default at once |
Empty select {} | Blocks forever |
Common Mistakes with the Go Select Statement
| Problem you see | Why it happens | How to fix it |
|---|---|---|
| fatal error: all goroutines are asleep – deadlock! | Because no goroutine will ever send. | So make sure a sender exists or add a timeout. |
| Loop never ends | Because break only left the select. | So use a labelled break or return. |
| CPU usage is 100% | Because a default case spins in a loop. | So remove default or add a ticker. |
In fact, these patterns appear in many backend interviews. So test yourself with my Go interview questions and answers.
Official Resources
These pages come straight from the Go team, so they stay accurate and up to date.
- Go spec: Select statements
- A Tour of Go: Select
- Go blog: Go Concurrency Patterns: Timing out, moving on
Quick Recap
- The Go select statement waits on many channel operations at once.
- It runs the first ready case, or a random one if several are ready.
time.Afteradds a simple timeout.- A
defaultcase makes select non-blocking. - Use a labelled break to leave a for-select loop.
What’s Next?
At last, you can coordinate many channels. Next, we will combine goroutines, channels and select into a worker pool that processes jobs in parallel.
Next lesson: Go Worker Pool Pattern Explained Simply


