1

I have a reaction struct like this

type Reaction struct {
    Id           uint   `json:"id" form:"id"`
    ReactionType uint   `json:"reactionType" form:"reactionType"`
    PostId       uint   `json:"postId" form:"postId"`
    ReactorId    uint   `json:"reactorId" form:"reactorId"`
    CreatedAt    string `json:"createdAt" form:"createdAt"`
    UpdatedAt    string `json:"updatedAt" form:"createdAt"`
}

And I have a function that consumes an API that should return a slice of Reaction

var myClient = &http.Client{Timeout: 7 * time.Second}

func getJson(url string, result interface{}) error {
  req, _ := http.NewRequest("GET", url, nil)
  resp, err := myClient.Do(req)

  if err != nil {
     return fmt.Errorf("cannot fetch URL %q: %v", url, err)
  }
  defer resp.Body.Close()

  if resp.StatusCode != http.StatusOK {
    return fmt.Errorf("unexpected http GET status: %s", resp.Status)
  }

  err = json.NewDecoder(resp.Body).Decode(result)
  if err != nil {
     return fmt.Errorf("cannot decode JSON: %v", err)
  }
  return nil
}

But somehow it failed to display the array/slice of objects that I want to retrieve, I got no data at all. Where did I miss?

func main(){
   ..
   ..
   var reactList []Reaction
   getJson("http://localhost:80/reactions", reactList)

   for _, r := range reactList {
        fmt.Print(r.ReactionType)
    }
}

and this is the original responses

[
  {
    "id": 55,
    "reactionType": 5,
    "reactorId": 2,
    "postId": 4,
    "createdAt": "2017-11-18 14:23:29",
    "updatedAt": ""
  },
  {
    "id": 56,
    "reactionType": 5,
    "reactorId": 3,
    "postId": 4,
    "createdAt": "2017-11-18 14:23:42",
    "updatedAt": ""
  },
  {
    "id": 57,
    "reactionType": 4,
    "reactorId": 4,
    "postId": 4,
    "createdAt": "2017-11-18 14:23:56",
    "updatedAt": ""
  }
]
mkopriva
  • 35,176
  • 4
  • 57
  • 71

3 Answers3

3

As pointed out in the comments you need to pass a pointer to getJson so that it can actually modify the contents of slice.

getJson("http://localhost:80/reactions", &reactList)

This is a close representation of what you have https://play.golang.org/p/_PnNK64giE, just see where things aren't in place in your case.

mkopriva
  • 35,176
  • 4
  • 57
  • 71
Ravi R
  • 1,692
  • 11
  • 16
0

Check this playground: https://play.golang.org/p/VjocUtiDRN

Maybe this is what you want. So you should make your function getJson(btw getJSON is better name in Golang naming convention) takes []Reaction parameter, instead of interface{}. Then you can unmarshal your array-like response into slice of Reactions.

hallazzang
  • 651
  • 8
  • 18
0

The main error here is that you're passing result without pointer. Should be:

getJson("http://localhost:80/reactions", &reactList)
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
  • 1
    this is incorrect, if he passes a non pointer to `getJson` and passes a pointer to `Decode` **inside** `getJson`, he'll still end up with an empty slice after `getJson` returns. – mkopriva Nov 22 '17 at 10:37