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