helloworld

This commit is contained in:
2024-05-25 00:30:38 -04:00
parent 4f4cc61f00
commit 80e3792b30
4 changed files with 48 additions and 18 deletions

3
helloworld/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module hello
go 1.22.1

36
helloworld/hello.go Normal file
View File

@@ -0,0 +1,36 @@
package main
import "fmt"
const (
spanish = "Spanish"
french = "French"
englishHelloPrefix = "Hello, "
spanishHelloPrefix = "Hola, "
frenchHelloPrefix = "Bonjour, "
)
func Hello(name string, language string) string {
if name == "" {
name = "World"
}
return greetingPrefix(language) + name
}
func greetingPrefix(language string) (prefix string) {
switch language {
case spanish:
prefix = spanishHelloPrefix
case french:
prefix = frenchHelloPrefix
default:
prefix = englishHelloPrefix
}
return
}
func main() {
fmt.Println(Hello("world", ""))
}

33
helloworld/hello_test.go Normal file
View File

@@ -0,0 +1,33 @@
package main
import "testing"
func TestHello(t *testing.T) {
t.Run("saying hello to people", func(t *testing.T) {
got := Hello("Chris", "")
want := "Hello, Chris"
assertCorrectMessage(t, got, want)
})
t.Run("say 'Hello, World' when an empty string is supplied", func(t *testing.T) {
got := Hello("", "")
want := "Hello, World"
assertCorrectMessage(t, got, want)
})
t.Run("in Spanish", func(t *testing.T) {
got := Hello("Elodie", "Spanish")
want := "Hola, Elodie"
assertCorrectMessage(t, got, want)
})
t.Run("in French", func(t *testing.T) {
got := Hello("Michael", "French")
want := "Bonjour, Michael"
assertCorrectMessage(t, got, want)
})
}
func assertCorrectMessage(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
}
}