Get explained
This commit is contained in:
parent
1657b9531d
commit
3fe315e0f9
2 changed files with 35 additions and 0 deletions
2
main.go
2
main.go
|
@ -49,6 +49,7 @@ import (
|
|||
"golesson/loops"
|
||||
"golesson/maps"
|
||||
"golesson/pointers"
|
||||
"golesson/restful"
|
||||
"golesson/slices"
|
||||
"golesson/string_functions"
|
||||
"golesson/structs"
|
||||
|
@ -128,4 +129,5 @@ func main() {
|
|||
fmt.Println(error_handling.GuessIt2(102))
|
||||
string_functions.Demo1()
|
||||
string_functions.Demo2()
|
||||
restful.Demo1()
|
||||
}
|
||||
|
|
33
restful/demo1.go
Normal file
33
restful/demo1.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
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)
|
||||
}
|
Loading…
Add table
Reference in a new issue