go-by-test/countdown/countdown.go

33 lines
478 B
Go
Raw Normal View History

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()
}
type DefaultSleeper struct{}
func (d *DefaultSleeper) Sleep() {
time.Sleep(1 * time.Second)
}
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)
}