Created
January 31, 2022 21:23
-
-
Save tibers/0470b543312722f8769406a6c783cd8f to your computer and use it in GitHub Desktop.
V2 of the stackovergo
This file contains 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/ioutil" | |
"log" | |
"net/http" | |
"time" | |
) | |
type AutoGenerated struct { | |
Items []Items `json:"items"` | |
HasMore bool `json:"has_more"` | |
QuotaMax int `json:"quota_max"` | |
QuotaRemaining int `json:"quota_remaining"` | |
} | |
type Owner struct { | |
AccountID int `json:"account_id"` | |
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"` | |
} | |
type Items struct { | |
Tags []string `json:"tags"` | |
Owner Owner `json:"owner,omitempty"` | |
ViewCount int `json:"view_count"` | |
Score int `json:"score"` | |
LastActivityDate int `json:"last_activity_date"` | |
CreationDate int `json:"creation_date"` | |
ArticleID int `json:"article_id"` | |
ArticleType string `json:"article_type"` | |
Link string `json:"link"` | |
Title string `json:"title"` | |
LastEditDate int `json:"last_edit_date,omitempty"` | |
} | |
func main() { | |
url := "https://api.stackexchange.com/2.3/articles?page=1&order=desc&sort=activity&site=stackoverflow" | |
soClient := http.Client{ | |
Timeout: time.Second * 2, // Timeout after 2 seconds | |
} | |
req, err := http.NewRequest(http.MethodGet, url, nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
req.Header.Set("User-Agent", "http-tutorial") | |
res, getErr := soClient.Do(req) | |
if getErr != nil { | |
log.Fatal(getErr) | |
} | |
if res.Body != nil { | |
defer res.Body.Close() | |
} | |
body, readErr := ioutil.ReadAll(res.Body) | |
if readErr != nil { | |
log.Fatal(readErr) | |
} | |
soitems := AutoGenerated{} | |
jsonErr := json.Unmarshal(body, &soitems) | |
if jsonErr != nil { | |
log.Fatal(jsonErr) | |
} | |
// fmt.Println(soitems.Items) | |
for index := range soitems.Items { | |
if soitems.Items[index].Score > 0 { | |
fmt.Println(soitems.Items[index]) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment