dtlrp119999 2018-06-11 10:23
浏览 53
已采纳

每当引用从函数返回的切片或映射时都为空指针

It usually will be something like this

func main() {
   my_map := myFunc()
   fmt.Println(my_map)
}

func myFunc() map[string]interface{} {
   // .... create a map....
   return map
}

when I run patterns like this, I almost always get a segfault when I try and print out my_map.

This also happens if I return a []byte type.

Now here's the thing: I think understand why returning a []byte that was created inside myFunc() would result in a segfault when printing it in main: The slice is referencing an array that was created inside myFunc, so as soon as myFunc returns, that array is deleted by garbage collection, and hence the segfault.

I'm going to assume something similar is happening with the map.

In C++, it would simply create a copy of the map, and return that --> no problem. Same if i returned an array.

Why, in go, can't I simply return a created map, and what would be the best way to return a map (created inside a function) without resulting in a segfault as soon as that function returns to main?

func twitterRequest(query string, token string) map[string]interface{} {
    req, err := http.NewRequest("GET", query, nil)
    if err != nil {
        log.Fatal(err.Error())
    }
    req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
    client := &http.Client{}
    resp, err2 := client.Do(req)
    if err2 != nil {
        log.Fatal(err.Error())
    }
    defer resp.Body.Close()
    respBody, _ := ioutil.ReadAll(resp.Body)
    var jsonResp interface{} // turns into map[string]interface{}
    err = json.Unmarshal(respBody, &jsonResp)
    if err != nil {
        log.Fatal("wrong")
    }
    return jsonResp.(map[string]interface{})
}

func searchTweets(queries string, token string) {
    builder := strings.Builder{}
    builder.WriteString(tweetSearchUrl)
    builder.WriteString("q=")
    builder.WriteString(queries)
    queryUrl := url.QueryEscape(builder.String())
    map1 := twitterRequest(queryUrl, token)
    fmt.Println(map1) // SEG FAULT HERE
}
  • 写回答

1条回答 默认 最新

  • doukefu1361 2018-06-11 10:44
    关注

    Returning values created inside a function in Go is perfectly OK and used commonly. The compiler uses escape analysis, and if it can't prove a value does not escape from the function, it will be allocated on the heap, and so will not be destroyed when the function returns, and can be used after the function has returned.

    The error can't come from the line printing the map (unless it contains a recursive data structure, which it doesn't since it's the result of a JSON unmarshaling). Printing empty or even nil maps is possible and results in no error.

    Most likely the error is in your twitterRequest() function, possibly when you do a type assertion returning the value:

    return jsonResp.(map[string]interface{})
    

    This type assertion results in a run-time panic if jsonResp is nil, or the value it holds is not of type map[string]interface{}. It may be nil if the response body is invalid JSON, and it may be a value of other type if the response body is valid JSON, but it is a JSON array for example.

    First, check all errors, including the one when reading the body:

    respBody, _ := ioutil.ReadAll(resp.Body)
    

    Do it like:

    respBody, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        // Handle error, optionally return
    }
    

    You should also use the special form of type assertion:

    m, ok := jsonResp.(map[string]interface{})
    if !ok {
        // jsonResp is nil or is not a map!
        // Handle this error case and return
    }
    
    return m // all ok
    

    This special form of type assertion will never panic, instead if the assertion does not hold, the ok variable will be false, which you can check and handle it properly. If ok is true, that means jsonResp was not nil, and the value inside it was indeed a value of type map[string]interface{}, which is stored in m, which you can safely return.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分