Skip to content

Instantly share code, notes, and snippets.

@tibers
Last active December 1, 2021 22:23
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 tibers/cc438da179f6349d685a5c2d2e943e09 to your computer and use it in GitHub Desktop.
Save tibers/cc438da179f6349d685a5c2d2e943e09 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)
// https://mholt.github.io/json-to-go/
// This is the coolest thing in the world
type Response struct {
Items []struct {
Tags []string `json:"tags"`
Owner struct {
Reputation int `json:"reputation"`
UserID int `json:"user_id"`
UserType string `json:"user_type"`
AcceptRate int `json:"accept_rate"`
ProfileImage string `json:"profile_image"`
DisplayName string `json:"display_name"`
Link string `json:"link"`
} `json:"owner"`
IsAnswered bool `json:"is_answered"`
ViewCount int `json:"view_count"`
AnswerCount int `json:"answer_count"`
Score int `json:"score"`
LastActivityDate int `json:"last_activity_date"`
CreationDate int `json:"creation_date"`
LastEditDate int `json:"last_edit_date"`
QuestionID int `json:"question_id"`
ContentLicense string `json:"content_license"`
Link string `json:"link"`
Title string `json:"title"`
} `json:"items"`
HasMore bool `json:"has_more"`
QuotaMax int `json:"quota_max"`
QuotaRemaining int `json:"quota_remaining"`
}
func main() {
res, err := http.Get("https://api.stackexchange.com/2.2/questions?site=stackoverflow")
if err != nil {
log.Fatal(err)
}
body, err := io.ReadAll(res.Body)
res.Body.Close()
if res.StatusCode > 299 {
log.Fatalf("Response failed with status code: %d and\nbody: %s\n", res.StatusCode, body)
}
if err != nil {
log.Fatal(err)
}
// fmt.Printf("%s", body)
var myquestion Response
json.Unmarshal([]byte(body), &myquestion)
b, err := json.MarshalIndent(myquestion, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment