sdf
This commit is contained in:
parent
0124fc6ace
commit
c1bae790ed
3
go_book/OMDBAPI/go.mod
Normal file
3
go_book/OMDBAPI/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module main
|
||||
|
||||
go 1.25.5
|
||||
50
go_book/OMDBAPI/main.go
Normal file
50
go_book/OMDBAPI/main.go
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
URL = "http://www.omdbapi.com"
|
||||
KEY = "fa22ab55"
|
||||
)
|
||||
|
||||
type SearchRes struct {
|
||||
Title string `json:"Title"`
|
||||
Year string `json:"Year"`
|
||||
Genre string `json:"Genre"`
|
||||
Ratings []Ratings
|
||||
}
|
||||
|
||||
type Ratings struct {
|
||||
Source string `json:"Source"`
|
||||
Value string `json:"Value"`
|
||||
}
|
||||
|
||||
type Plug any
|
||||
|
||||
type PlugStruct struct {}
|
||||
|
||||
func Search(s string) (SearchRes,error) {
|
||||
resp,err := http.Get(URL + "?apikey=" + KEY + "&t=" + s)
|
||||
if err != nil {
|
||||
return SearchRes{},err
|
||||
}
|
||||
|
||||
var res SearchRes
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
|
||||
fmt.Print("Error in unmarshalling ",err)
|
||||
}
|
||||
return res,nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
arg := os.Args[1]
|
||||
// fmt.Println(arg)
|
||||
fmt.Println(Search(arg))
|
||||
}
|
||||
|
||||
|
|
@ -1,30 +1,59 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"go.uber.org/zap/buffer"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func addCommas(n int) string {
|
||||
str := strconv.Itoa(n)
|
||||
const URL = "https://api.github.com/search/issues"
|
||||
|
||||
// Add commas from right to left
|
||||
var result buffer.Buffer
|
||||
for i := range str {
|
||||
if i > 0 && (len(str)-i)%3 == 0 {
|
||||
fmt.Fprint(&result,",")
|
||||
}
|
||||
result.WriteByte(str[i])
|
||||
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)
|
||||
}
|
||||
|
||||
return result.String()
|
||||
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() {
|
||||
numbers := []int{12345, 1234567, 987654321, 12345, 123, 0}
|
||||
|
||||
for _, num := range numbers {
|
||||
fmt.Printf("%d -> %s\n", num, addCommas(num))
|
||||
result,err := IssuerSearch(os.Args[1:])
|
||||
if err != nil {
|
||||
fmt.Println("Error in IssuerSearch")
|
||||
}
|
||||
fmt.Printf("%#v\n",result)
|
||||
fmt.Println(result)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user