countdown: add basic countdown

This commit is contained in:
vinchent 2024-09-17 21:10:09 +02:00
parent 5f02a8fbde
commit d7840e9cb7
3 changed files with 55 additions and 0 deletions

21
countdown/countdown.go Normal file
View File

@ -0,0 +1,21 @@
package countdown
import (
"fmt"
"io"
"time"
)
const (
finalWord = "Go!"
countdownStart = 3
)
func Countdown(out io.Writer) {
for i := countdownStart; i > 0; i-- {
fmt.Fprintf(out, "%d\n", i)
// This is difficult to test!
time.Sleep(1 * time.Second)
}
fmt.Fprint(out, finalWord)
}

View File

@ -0,0 +1,22 @@
package countdown
import (
"bytes"
"testing"
)
func TestCountdown(t *testing.T) {
buffer := &bytes.Buffer{}
Countdown(buffer)
got := buffer.String()
want := `3
2
1
Go!`
if got != want {
t.Errorf("got %q want %q", got, want)
}
}

12
main.go Normal file
View File

@ -0,0 +1,12 @@
package main
import (
"gobytest/countdown"
"os"
)
func main() {
// greet.Greet(os.Stdout, "Elodie")
countdown.Countdown(os.Stdout)
}