-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocking.go
52 lines (43 loc) · 955 Bytes
/
mocking.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package countdown
import (
"fmt"
"io"
"strconv"
"time"
)
const finalword = "GO!"
const countdownstart = 3
// Sleeper interface with Sleep() function
type Sleeper interface {
Sleep()
}
// DefaultSleeper default sleeper
type DefaultSleeper struct{}
// Sleep waits for a second
func (d *DefaultSleeper) Sleep() {
time.Sleep(1 * time.Second)
}
// ConfigurableSleeper struct with duration and sleep
type ConfigurableSleeper struct {
duration time.Duration
sleep func(time.Duration)
}
// Sleep waits for given seconds
func (c *ConfigurableSleeper) Sleep() {
c.sleep(c.duration)
}
// Countdown counts to given time
func Countdown(writer io.Writer, sleeper Sleeper) {
for i := countdownstart; i > 0; i-- {
sleeper.Sleep()
fmt.Fprint(writer, strconv.Itoa(i)+"\n")
}
sleeper.Sleep()
fmt.Fprint(writer, finalword)
}
/*
func main() {
sleeper := &ConfigurableSleeper{1 * time.Second, time.Sleep}
Countdown(os.Stdout, sleeper)
}
*/