Select

Select waits on multiple channels. This can be combined with goroutines. Select is like the switch instruction, but for channels.

For example, two routines are started simultaneously and each one writes data to a channel. As it runs concurrently, it may happen that one task is completed before the other.


select {
case msg1 := <- ch1:
    // ...
case <- ch2:
    // ...
}

                        

The select statement will make the program wait for both tasks to be completed, but this does not mean that both tasks are always completed in chronological order.

select.go