Middle+
In what order does the select operator read from multiple channels?
sobes.tech AI
Answer from AI
In Go, the select operator chooses a ready channel at random if there are multiple. If no channel is ready, select blocks (if there is no default).
The order of reading from multiple channels is not fixed and not guaranteed. This is done to prevent deadlocks and ensure fair distribution.
Example:
select {
case msg1 := <-chan1:
fmt.Println("Received from chan1:", msg1)
case msg2 := <-chan2:
fmt.Println("Received from chan2:", msg2)
default:
fmt.Println("No channels are ready")
}
If both chan1 and chan2 are ready, Go will randomly choose one of them.