From d7840e9cb7fe364e172009cd06a7a88c89fd1818 Mon Sep 17 00:00:00 2001 From: vinchent Date: Tue, 17 Sep 2024 21:10:09 +0200 Subject: [PATCH] countdown: add basic countdown --- countdown/countdown.go | 21 +++++++++++++++++++++ countdown/countdown_test.go | 22 ++++++++++++++++++++++ main.go | 12 ++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 countdown/countdown.go create mode 100644 countdown/countdown_test.go create mode 100644 main.go diff --git a/countdown/countdown.go b/countdown/countdown.go new file mode 100644 index 0000000..4cd054b --- /dev/null +++ b/countdown/countdown.go @@ -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) +} diff --git a/countdown/countdown_test.go b/countdown/countdown_test.go new file mode 100644 index 0000000..524b01c --- /dev/null +++ b/countdown/countdown_test.go @@ -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) + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..f97a750 --- /dev/null +++ b/main.go @@ -0,0 +1,12 @@ +package main + +import ( + "gobytest/countdown" + "os" +) + +func main() { + // greet.Greet(os.Stdout, "Elodie") + + countdown.Countdown(os.Stdout) +}