select — wait on multiple channel operations.
1select {2case msg1 := <-ch1:3 fmt.Println("Received from ch1:", msg1)4case msg2 := <-ch2:5 fmt.Println("Received from ch2:", msg2)6case ch3 <- 42:7 fmt.Println("Sent to ch3")8case <-time.After(5 * time.Second):9 fmt.Println("Timeout")10default:11 fmt.Println("No communication")12}
Key points:
default — non-blocking.time.After — timeout.Use cases: