Popraw ten kod, aby prawidłowo wyświetlał dane z : githubusername, userid, websiteurl,…
- User
Popraw ten kod, aby prawidłowo wyświetlał dane z : github_username, user_id, website_url, profile_image
package main
import ( "encoding/json" "fmt" "io/ioutil" "net/http" "time"
)
type Profile struct { ProfileViews int
json:"profile_views"Followers intjson:"followers"}type Article struct { Title string
json:"title"PageViews intjson:"page_views"Comments intjson:"comments"CommentsCount intjson:"comments_count"PublicReactionsCount intjson:"public_reactions_count"PublishedTimestamp time.Timejson:"published_timestamp"PositiveReactionsCount intjson:"positive_reactions_count"CoverImage string `json:"cover_image"` SocialImage string `json:"social_image"` CreatedAt time.Time `json:"created_at"` PublishedAt time.Time `json:"published_at"` LastCommentAt time.Time `json:"last_comment_at"` ReadingTimeMinutes int `json:"reading_time_minutes"` TagList []string `json:"tag_list"` GithubUsername string `json:"github_username"` UserID int `json:"user_id"` WebsiteURL string `json:"website_url"` ProfileImage string `json:"profile_image"`}
func main() { apiKey := "key" url := "https://dev.to/api/articles?username=dev"
req, err := http.NewRequest("GET", url, nil) if err != nil { fmt.Println("Blad tworzenia zadania:", err) return}
req.Header.Set("Authorization", "Bearer "+apiKey) client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Println("Blad wykonania zadania:", err) return } defer resp.Body.Close() if resp.StatusCode == http.StatusUnauthorized { fmt.Println("Blad uwierzytelniania. Sprawdz poprawnosc klucza API.") return } body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Blad odczytu odpowiedzi:", err) return } var articles []Article err = json.Unmarshal(body, &articles) if err != nil { // Sprawdzamy, czy otrzymalismy tablicę JSON var article Article err = json.Unmarshal(body, &article) if err != nil { fmt.Println("Blad deserializacji JSON:", err) return } // Jesli deserializacja pojedynczego obiektu się powiodla, to przypisujemy go do tablicy articles = []Article{article} } fmt.Println("Ilosc moich artykulow:", len(articles)) fmt.Println("Tytuly artykulow:") for _, article := range articles { fmt.Println("- " + article.Title) } totalPageViews := 0 totalComments := 0 for _, article := range articles { totalPageViews += article.PageViews totalComments += article.Comments fmt.Println("comments_count:", article.CommentsCount) fmt.Println("public_reactions_count:", article.PublicReactionsCount) fmt.Println("published_timestamp:", article.PublishedTimestamp) fmt.Println("positive_reactions_count:", article.PositiveReactionsCount) fmt.Println("created_at:", article.CreatedAt) fmt.Println("published_at:", article.PublishedAt) fmt.Println("last_comment_at:", article.LastCommentAt) fmt.Println("reading_time_minutes:", article.ReadingTimeMinutes) fmt.Println("tag_list:", article.TagList) fmt.Println() fmt.Println("github_username:", article.GithubUsername) fmt.Println("user_id:", article.UserID) fmt.Println("website_url:", article.WebsiteURL) fmt.Println("profile_image:", article.ProfileImage) fmt.Println("cover_image:", article.CoverImage) fmt.Println("social_image:", article.SocialImage) } fmt.Println("Ilosc wyswietlen artykulow:", totalPageViews) fmt.Println("Ilosc komentarzy:", totalComments) - Assistant