Middle+
Millises järjekorras loeb select operaator mitmest kanalist?
sobes.tech AI
Vastus AI-lt
In Go, the select operator randomly chooses one of the ready (ready) channels if there are multiple. If none are 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 blocking 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.