

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
The default case in a select statement executes immediately if no other channel has a value ready. A default case stops the select statement from blocking.
select {
case v := <-ch:
// use v
default:
// receiving from ch would block
// so do something else
}
Sometimes you want to ignore a channel's value. You can do this by not binding it to a variable:
select {
case <-ch:
// event received; value ignored
default:
// so do something else
}
Alternatively, you can use the blank identifier _ to ignore the value:
select {
case _ = <-ch:
// event received; value ignored
default:
// so do something else
}
The functions take a time.Duration as an argument. For example:
time.Tick(500 * time.Millisecond)
If you don't add time.Millisecond (or another unit), it will default to nanoseconds. That's – taking a wild guess here – probably faster than you want it to be.
A channel can be marked as read-only by casting it from a chan to a <-chan type. For example:
func main() {
ch := make(chan int)
readCh(ch)
}
func readCh(ch <-chan int) {
// ch can only be read from
// in this function
}
The same goes for write-only channels, but the arrow's position moves.
func writeCh(ch chan<- int) {
// ch can only be written to
// in this function
}
Like all good back-end engineers, we frequently save backup snapshots of the Textio database.
Complete the saveBackups function.
It should use a loop to read values from the snapshotTicker and saveAfter channels simultaneously and continuously.