go-by-test/helloworld/hello.go

51 lines
947 B
Go
Raw Normal View History

2024-09-10 09:47:40 +00:00
package helloworld
2024-09-10 09:06:51 +00:00
2024-09-10 09:16:15 +00:00
const (
2024-09-10 09:35:30 +00:00
english = "English"
spanish = "Spanish"
2024-09-10 09:44:21 +00:00
)
2024-09-10 09:35:30 +00:00
2024-09-10 09:44:21 +00:00
var (
helloPrefixMap map[string]string
defaultWorldMap map[string]string
2024-09-10 09:16:15 +00:00
)
2024-09-10 09:44:21 +00:00
func initDefaults() {
initPrefixMap()
initWorldMap()
}
2024-09-10 09:35:30 +00:00
2024-09-10 09:44:21 +00:00
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]
2024-09-10 09:35:30 +00:00
}
func getHelloPrefixByLanguage(language string) string {
2024-09-10 09:44:21 +00:00
return helloPrefixMap[language]
2024-09-10 09:35:30 +00:00
}
func Hello(name, language string) string {
prefix := getHelloPrefixByLanguage(language)
2024-09-10 09:12:42 +00:00
if name == "" {
2024-09-10 09:35:30 +00:00
return prefix + getDefaultWorldByLanguage(language)
2024-09-10 09:12:42 +00:00
}
2024-09-10 09:35:30 +00:00
return prefix + name
2024-09-10 09:06:51 +00:00
}
2024-09-10 09:47:40 +00:00
// func main() {
// initDefaults()
// fmt.Println(Hello("Mary", "English"))
// }