Use the select statement with case clauses to block until one of multiple channels is ready for communication.
Use a select statement with multiple case clauses to wait on several channels simultaneously, executing the first one that becomes ready.
select {
case msg := <-ch1:
fmt.Println("Received from ch1:", msg)
case msg := <-ch2:
fmt.Println("Received from ch2:", msg)
default:
fmt.Println("No messages ready")
}
If you need to wait indefinitely until a channel is ready, omit the default case.
The select statement lets your program wait for multiple events at once, like listening to several phone lines simultaneously. It automatically picks the first line that rings and handles that call, ignoring the others until the next time you check. This prevents your program from freezing while waiting for a single specific event.