Skip to content

Instantly share code, notes, and snippets.

@smtalim
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smtalim/51f8d01cb545ee389dcb to your computer and use it in GitHub Desktop.
Save smtalim/51f8d01cb545ee389dcb to your computer and use it in GitHub Desktop.
Second Iteration
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type Item struct {
Author string `json:"author"`
Score int `json:"score"`
URL string `json:"url"`
Title string `json:"title"`
}
type response struct {
Data1 struct {
Children []struct {
Data2 Item `json:"data"`
} `json:"children"`
} `json:"data"`
}
func main() {
resp, err := http.Get("http://reddit.com/r/golang.json")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Fatal(resp.Status)
}
r := new(response)
err = json.NewDecoder(resp.Body).Decode(r)
if err != nil {
log.Fatal(err)
}
for _, child := range r.Data1.Children {
fmt.Println(child.Data2.Author)
fmt.Println(child.Data2.Score)
fmt.Println(child.Data2.URL)
fmt.Println(child.Data2.Title)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment