| // +build ignore,OMIT |
| |
| package main |
| |
| import ( |
| "encoding/json" |
| "fmt" |
| "log" |
| "net/http" |
| ) |
| |
| func main() { |
| resp, err := http.Get("http://reddit.com/r/golang.json") |
| if err != nil { |
| log.Fatal(err) |
| } |
| 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.Data.Children { |
| fmt.Println(child.Data.Title) |
| } |
| } |
| |
| type Item struct { |
| Title string |
| URL string |
| } |
| |
| type Response struct { |
| Data struct { |
| Children []struct { |
| Data Item |
| } |
| } |
| } |