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": ""
}
]