From 22944de3b3608d976e5d654f5b7cf4ae992e7af7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krejczinger=20=C3=81rp=C3=A1d?= Date: Sun, 15 Mar 2026 22:02:50 +0100 Subject: [PATCH] Handle empty string as "World" --- hello.go | 7 ++++++- hello_test.go | 20 +++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/hello.go b/hello.go index c72c826..aafd837 100644 --- a/hello.go +++ b/hello.go @@ -2,8 +2,13 @@ package main import "fmt" +const englishHelloPrefix = "Hello, " + func Hello(name string) string { - return "Hello, " + name + if name == "" { + name = "World" + } + return englishHelloPrefix + name } func main() { diff --git a/hello_test.go b/hello_test.go index 957b469..c8a873a 100644 --- a/hello_test.go +++ b/hello_test.go @@ -3,10 +3,20 @@ package main import "testing" func TestHello(t *testing.T) { - got := Hello("Hoborg") - want := "Hello, Hoborg" + t.Run("saying hello to people", func(t *testing.T) { + got := Hello("Hoborg") + want := "Hello, Hoborg" - if got != want { - t.Errorf("got %q want %q", got, want) - } + if got != want { + t.Errorf("got %q want %q", got, want) + } + }) + t.Run("say 'Hello, World' when an empty string is given", func(t *testing.T) { + got := Hello("") + want := "Hello, World" + + if got != want { + t.Errorf("got %q want %q", got, want) + } + }) }