Last active
December 1, 2021 22:23
-
-
Save tibers/cc438da179f6349d685a5c2d2e943e09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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