46 lines
702 B
Go
46 lines
702 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
const finalWord = "Go!"
|
|
const countdownStart = 3
|
|
|
|
type Sleeper interface {
|
|
Sleep()
|
|
}
|
|
|
|
type DefaultSleeper struct {
|
|
}
|
|
|
|
type ConfigurableSleeper struct {
|
|
duration time.Duration
|
|
sleep func(duration time.Duration)
|
|
}
|
|
|
|
func Countdown_main() {
|
|
sleeper := &ConfigurableSleeper{1 * time.Second, time.Sleep}
|
|
Countdown(os.Stdout, sleeper)
|
|
}
|
|
|
|
func (d *DefaultSleeper) Sleep() {
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
|
|
func Countdown(out io.Writer, sleeper Sleeper) {
|
|
for i := countdownStart; i > 0; i-- {
|
|
sleeper.Sleep()
|
|
fmt.Fprintln(out, i)
|
|
}
|
|
sleeper.Sleep()
|
|
fmt.Fprint(out, finalWord)
|
|
}
|
|
|
|
func (c *ConfigurableSleeper) Sleep() {
|
|
c.sleep(c.duration)
|
|
}
|