Different languages supported

This commit is contained in:
2026-03-15 22:52:20 +01:00
parent 3cae1bd3d7
commit ca6357acdb
2 changed files with 30 additions and 7 deletions

View File

@@ -2,15 +2,28 @@ package main
import "fmt"
const englishHelloPrefix = "Hello, "
const (
spanish = "Spanish"
french = "French"
englishHelloPrefix = "Hello, "
spanishHelloPrefix = "Hola, "
frenchHelloPrefix = "Bonjour, "
)
func Hello(name string) string {
func Hello(name, language string) string {
if name == "" {
name = "World"
}
if language == french {
return frenchHelloPrefix + name
} else if language == spanish {
return spanishHelloPrefix + name
} else {
return englishHelloPrefix + name
}
}
func main() {
fmt.Println(Hello("Hoborg"))
fmt.Println(Hello("Hoborg", ""))
}

View File

@@ -4,13 +4,23 @@ import "testing"
func TestHello(t *testing.T) {
t.Run("saying hello to people", func(t *testing.T) {
got := Hello("Hoborg")
got := Hello("Hoborg", "")
want := "Hello, Hoborg"
assertMessage(t, got, want)
})
t.Run("say 'Hello, World' when an empty string is given", func(t *testing.T) {
got := Hello("")
want := "Hello, Worldi"
got := Hello("", "")
want := "Hello, World"
assertMessage(t, got, want)
})
t.Run("hello in Spanish", func(t *testing.T) {
got := Hello("Elodie", "Spanish")
want := "Hola, Elodie"
assertMessage(t, got, want)
})
t.Run("hello in French", func(t *testing.T) {
got := Hello("Émile", "French")
want := "Bonjour, Émile"
assertMessage(t, got, want)
})
}