From 3fe315e0f9ec7e16727a07d67caa1611b337f009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20G=C3=B6r?= Date: Thu, 21 Mar 2024 15:17:35 +0300 Subject: [PATCH] Get explained --- main.go | 2 ++ restful/demo1.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 restful/demo1.go diff --git a/main.go b/main.go index 6315cef..9789ca2 100644 --- a/main.go +++ b/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() } diff --git a/restful/demo1.go b/restful/demo1.go new file mode 100644 index 0000000..c2b0402 --- /dev/null +++ b/restful/demo1.go @@ -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) +} \ No newline at end of file