learning_go/go_book/proj1/main.go
a.pivkin c1bae790ed sdf
2025-12-27 18:55:08 +03:00

59 lines
1.2 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
)
const URL = "https://api.github.com/search/issues"
type IssuerSearchReport struct {
Total int `json:"total_count"`
Incomplete_results bool `json:"incomplete_results"`
Created_at time.Time `json:"created_at"`
Items []Issue
}
type Issue struct {
Title string `json:"title"`
User User
}
type User struct {
Login string `json:"login"`
}
func IssuerSearch(s []string) (IssuerSearchReport,error) {
new_s := s[0]
resp,err := http.Get(URL + "?q=" + new_s)
if err != nil {
return IssuerSearchReport{},err
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return IssuerSearchReport{},fmt.Errorf("Error in request",resp.Status)
}
var result IssuerSearchReport
body,_ := io.ReadAll(resp.Body)
if err := json.Unmarshal(body,&result); err != nil {
resp.Body.Close()
return IssuerSearchReport{},nil
}
resp.Body.Close()
return result,nil
}
func main() {
result,err := IssuerSearch(os.Args[1:])
if err != nil {
fmt.Println("Error in IssuerSearch")
}
fmt.Printf("%#v\n",result)
fmt.Println(result)
}