dsb0003795
dsb0003795
2018-06-04 12:00
浏览 37
已采纳

与测试一起使用时,方法无法访问全局变量

I am facing problem while using Golang Testing.Global variable not accessible to methods. Following is the code snippet

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
}

I am getting following errors

panic: assignment to entry in nil map [recovered]

panic: assignment to entry in nil map

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpfqy5976
    dpfqy5976 2018-06-04 12:07
    已采纳

    From your comments it looks like you didn't really want to shadow the global variable map1, you just want to return it.

    So you probably want

    func f()(req *http.Request) (bool, map[string]string, error) {
        map1["key"]="value"
        return true, map1, nil
    }
    

    Returning three arguments, and a global variable among them, looks strange though. There's probably something ill designed.

    点赞 评论

相关推荐