与测试一起使用时方法无法访问全局变量
global variable not accessible to methods when used with testing
我在使用方法无法访问的 Golang
Testing.Global 变量时遇到问题。
以下是代码片段
test1.go
var map1 = make(map[string]string)
func f()(req *http.Request) (ismime bool, map1 map[string]string, err
error) {
map1["key"]="value"
return true,map1,nil
}
我收到以下错误
panic: assignment to entry in nil map [recovered]
panic: assignment to entry in nil map
从您的评论来看,您似乎并不是真的想要隐藏全局变量 map1
,您只是想 return 它。
所以你可能想要
func f()(req *http.Request) (bool, map[string]string, error) {
map1["key"]="value"
return true, map1, nil
}
返回三个参数和其中的一个全局变量,虽然看起来很奇怪。可能设计有问题。
我在使用方法无法访问的 Golang
Testing.Global 变量时遇到问题。
以下是代码片段
test1.go
var map1 = make(map[string]string)
func f()(req *http.Request) (ismime bool, map1 map[string]string, err
error) {
map1["key"]="value"
return true,map1,nil
}
我收到以下错误
panic: assignment to entry in nil map [recovered]
panic: assignment to entry in nil map
从您的评论来看,您似乎并不是真的想要隐藏全局变量 map1
,您只是想 return 它。
所以你可能想要
func f()(req *http.Request) (bool, map[string]string, error) {
map1["key"]="value"
return true, map1, nil
}
返回三个参数和其中的一个全局变量,虽然看起来很奇怪。可能设计有问题。