reorgniaze the repo

This commit is contained in:
2024-09-10 11:47:40 +02:00
parent b50e202a97
commit 268677b9ad
3 changed files with 7 additions and 11 deletions

50
helloworld/hello.go Normal file
View File

@ -0,0 +1,50 @@
package helloworld
const (
english = "English"
spanish = "Spanish"
)
var (
helloPrefixMap map[string]string
defaultWorldMap map[string]string
)
func initDefaults() {
initPrefixMap()
initWorldMap()
}
func initPrefixMap() {
helloPrefixMap = make(map[string]string)
helloPrefixMap[english] = "Hello "
helloPrefixMap[spanish] = "Hola "
}
func initWorldMap() {
defaultWorldMap = make(map[string]string)
defaultWorldMap[english] = "world"
defaultWorldMap[spanish] = "Mundo"
}
func getDefaultWorldByLanguage(language string) string {
return defaultWorldMap[language]
}
func getHelloPrefixByLanguage(language string) string {
return helloPrefixMap[language]
}
func Hello(name, language string) string {
prefix := getHelloPrefixByLanguage(language)
if name == "" {
return prefix + getDefaultWorldByLanguage(language)
}
return prefix + name
}
// func main() {
// initDefaults()
// fmt.Println(Hello("Mary", "English"))
// }

31
helloworld/hello_test.go Normal file
View File

@ -0,0 +1,31 @@
package helloworld
import "testing"
func TestHello(t *testing.T) {
initDefaults()
t.Run("Hello someone", func(t *testing.T) {
got := Hello("Jason", "English")
exp := "Hello Jason"
assertCorrectMsg(t, got, exp)
})
t.Run("Hello default", func(t *testing.T) {
got := Hello("", "English")
exp := "Hello world"
assertCorrectMsg(t, got, exp)
})
t.Run("Hello default", func(t *testing.T) {
got := Hello("Elodie", "Spanish")
exp := "Hola Elodie"
assertCorrectMsg(t, got, exp)
})
}
func assertCorrectMsg(t testing.TB, got, exp string) {
t.Helper()
if got != exp {
t.Errorf("got %q expected %q", got, exp)
}
}