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 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号