46 lines
720 B
Go
46 lines
720 B
Go
package countdown
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
finalWord = "Go!"
|
|
countdownStart = 3
|
|
)
|
|
|
|
type Sleeper interface {
|
|
Sleep()
|
|
}
|
|
|
|
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,
|
|
}
|
|
}
|
|
|
|
func (s *ConfigurableSleeper) Sleep() {
|
|
s.sleep(s.duration)
|
|
}
|
|
|
|
func Countdown(out io.Writer, sleeper Sleeper) {
|
|
// XXX: The sequence is not tested
|
|
for i := countdownStart; i > 0; i-- {
|
|
fmt.Fprintf(out, "%d\n", i)
|
|
// This is difficult to test!
|
|
sleeper.Sleep()
|
|
}
|
|
fmt.Fprint(out, finalWord)
|
|
}
|