我聽說過很多次,你應該避免全局變量。當使用全局變量
在我的例子,我只宣佈全球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")
}