Select Statement in Go

From the Go cheat sheet · Concurrency · verified Jul 2026

Select Statement

Multiplex channel operations with select

go
// Select waits on multiple channels
select {
case msg := <-ch1:
    fmt.Println("From ch1:", msg)
case msg := <-ch2:
    fmt.Println("From ch2:", msg)
case ch3 <- "hello":
    fmt.Println("Sent to ch3")
default:
    fmt.Println("No channel ready")
}
🎯 select blocks until one case is ready (random if multiple)
⏱️ Use time.After for timeout patterns
💡 default case makes select non-blocking
📌 Common for fan-in, timeouts, and cancellation

More Go tasks

Back to the full Go cheat sheet