March 2019
Beginner to intermediate
324 pages
7h 17m
English
The select statement is an important construct in Go. It allows you to control multiple channels at the same time. With select, you can send or receive values to different channels, and then execute code based on the channel that unblocks the first.
This will be best explained by an example; let's take a look at the following piece of code:
select { case i := <-ch: fmt.Println("Received value:", i) case <-time.After(1 * time.Second): fmt.Println("timed out") }
In the preceding example, we utilized the select statement to exercise control over two different channels. The first channel is called ch, which we attempted to receive a value from. In comparison, the second channel is produced from the time.After() function. ...
Read now
Unlock full access