

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Concurrency
incomplete
2: Channels
incomplete
3: Channels
incomplete
4: Buffered Channels
incomplete
5: Closing Channels in Go
incomplete
6: Range
incomplete
7: Select
incomplete
8: Select Default Case
incomplete
9: Channels Review
incomplete
10: Ping Pong
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Sometimes we have a single goroutine listening to multiple channels and want to process data in the order it comes through each channel.
A select statement is used to listen to multiple channels at the same time. It is similar to a switch statement but for channels.
select {
case i, ok := <-chInts:
if ok {
fmt.Println(i)
}
case s, ok := <-chStrings:
if ok {
fmt.Println(s)
}
}
The first channel with a value ready to be received will fire and its body will execute. If multiple channels are ready at the same time then one is chosen randomly. In the example above, the ok variable is a boolean. It is true while the channel is open, and false when the channel is closed by the sender.
Complete the logMessages function. It should: