Select Statement and Channel Patterns
The select
statement allows waiting on multiple channel operations.
- It chooses a case that is ready and blocks if none are.
- Useful in fan-in/fan-out patterns, timeout management, and more.
- Acts like a
switch
but for channels.
select {
case msg1 := <-ch1:
fmt.Println("Received", msg1)
case msg2 := <-ch2:
fmt.Println("Received", msg2)
default:
fmt.Println("No message received")
}