mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-16 18:21:16 +00:00
first
This commit is contained in:
46
course/13-channels/exercises/8-select_default/complete.go
Normal file
46
course/13-channels/exercises/8-select_default/complete.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func saveBackups(snapshotTicker, saveAfter <-chan time.Time) {
|
||||
for {
|
||||
select {
|
||||
case <-snapshotTicker:
|
||||
takeSnapshot()
|
||||
case <-saveAfter:
|
||||
saveSnapshot()
|
||||
return
|
||||
default:
|
||||
waitForData()
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TEST SUITE - Don't touch below this line
|
||||
|
||||
func takeSnapshot() {
|
||||
fmt.Println("Taking a backup snapshot...")
|
||||
}
|
||||
|
||||
func saveSnapshot() {
|
||||
fmt.Println("All backups saved!")
|
||||
}
|
||||
|
||||
func waitForData() {
|
||||
fmt.Println("Nothing to do, waiting...")
|
||||
}
|
||||
|
||||
func test() {
|
||||
snapshotTicker := time.Tick(800 * time.Millisecond)
|
||||
saveAfter := time.After(2800 * time.Millisecond)
|
||||
saveBackups(snapshotTicker, saveAfter)
|
||||
fmt.Println("===========================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test()
|
||||
}
|
||||
Reference in New Issue
Block a user