go-by-test/helloworld/hello.go
2024-09-10 11:50:18 +02:00

51 lines
947 B
Go

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"))
// }