gokursu/restful/demo1.go

33 lines
548 B
Go
Raw Normal View History

2024-03-21 12:17:35 +00:00
package restful
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Todo struct{
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
func Demo1() {
response, err := http.Get("https://jsonplaceholder.typicode.com/todos/1")
if err != nil{
fmt.Println(err)
}
defer response.Body.Close()
bodyBytes,_ := ioutil.ReadAll(response.Body)
bodyString := string(bodyBytes)
fmt.Println(bodyString)
var todo Todo
json.Unmarshal(bodyBytes, &todo)
fmt.Println(todo)
}