我听说过很多次,你应该避免全局变量。当使用全局变量
在我的例子,我只宣布全球myTypes
变量,以避免一遍又一遍的声明变量函数调用或类似的东西。
这是它应该怎么做?有没有更好的办法?更可测试的方法?
var myTypes = map[string]string{
"type1": "tpl1",
"type2": "tpl2",
}
func AFunc(someType string) string {
fmt.Sprintf("this is your type %s", myTypes[someType])
}
func main() {
AFunc("type1")
}