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
}
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.