2024-09-17 19:10:09 +00:00
|
|
|
package countdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
finalWord = "Go!"
|
|
|
|
countdownStart = 3
|
|
|
|
)
|
|
|
|
|
2024-09-17 19:20:33 +00:00
|
|
|
type Sleeper interface {
|
|
|
|
Sleep()
|
|
|
|
}
|
|
|
|
|
2024-09-17 19:43:48 +00:00
|
|
|
type ConfigurableSleeper struct {
|
|
|
|
duration time.Duration
|
|
|
|
sleep func(time.Duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfigurableSleeper(
|
|
|
|
duration time.Duration,
|
|
|
|
sleep func(time.Duration),
|
|
|
|
) *ConfigurableSleeper {
|
|
|
|
return &ConfigurableSleeper{
|
|
|
|
duration: duration,
|
|
|
|
sleep: sleep,
|
|
|
|
}
|
|
|
|
}
|
2024-09-17 19:20:33 +00:00
|
|
|
|
2024-09-17 19:43:48 +00:00
|
|
|
func (s *ConfigurableSleeper) Sleep() {
|
|
|
|
s.sleep(s.duration)
|
2024-09-17 19:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Countdown(out io.Writer, sleeper Sleeper) {
|
|
|
|
// XXX: The sequence is not tested
|
2024-09-17 19:10:09 +00:00
|
|
|
for i := countdownStart; i > 0; i-- {
|
|
|
|
fmt.Fprintf(out, "%d\n", i)
|
|
|
|
// This is difficult to test!
|
2024-09-17 19:20:33 +00:00
|
|
|
sleeper.Sleep()
|
2024-09-17 19:10:09 +00:00
|
|
|
}
|
|
|
|
fmt.Fprint(out, finalWord)
|
|
|
|
}
|