go-learning/tour_of_go/l7/default-selection.go

28 lines
495 B
Go
Raw Normal View History

2025-09-26 14:43:54 +00:00
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
tick := time.Tick(100 * time.Millisecond)
boom := time.After(500 * time.Millisecond)
elapsed := func() time.Duration {
return time.Since(start).Round(time.Millisecond)
}
for {
select {
case <- tick:
fmt.Printf("[%6s] tick.\n", elapsed())
case <- boom:
fmt.Printf("[%6s] BOOM!\n", elapsed())
return
default:
fmt.Printf("[%6s] .\n", elapsed())
time.Sleep(50 * time.Millisecond)
}
}
}