How to use select with channels

Use the select statement to wait on multiple channel operations simultaneously, executing the first one that becomes ready.

You use the select statement to wait on multiple channel operations simultaneously, blocking until one of them is ready. This pattern is essential for handling timeouts, merging streams, or coordinating goroutines without race conditions.

select {
case msg := <-ch1:
	fmt.Println("Received from ch1:", msg)
case ch2 <- "data":
	fmt.Println("Sent to ch2")
default:
	fmt.Println("No channel ready")
}

The select statement picks a random ready case if multiple are available, or blocks if none are ready (unless a default case exists).