Default to the first message if name is empty

This commit is contained in:
Oliver Davies 2022-01-26 18:00:00 +00:00
parent 4c1fb0ec5a
commit 0f3e93e7d5
2 changed files with 13 additions and 0 deletions

View file

@ -5,6 +5,10 @@ import "fmt"
const englishHelloPrefix = "Hello, "
func Hello(name string) string {
if name == "" {
name = "World"
}
return englishHelloPrefix + name
}

View file

@ -11,4 +11,13 @@ func TestHello(t *testing.T) {
t.Errorf("got %q want %q", got, want)
}
})
t.Run("say 'Hello, World' when an empty string is supplied", func (t *testing.T) {
got := Hello("")
want := "Hello, World"
if got != want {
t.Errorf("got %q want %q", got, want)
}
})
}