Get explained

This commit is contained in:
Mert Gör ☭ 2024-03-21 15:17:35 +03:00
parent 1657b9531d
commit 3fe315e0f9
Signed by: hwpplayer1
GPG Key ID: 03E547D043AB6C8F
2 changed files with 35 additions and 0 deletions

View File

@ -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
View 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)
}