How to get JSON response from HTTP GET request in Go
Hi everyone! I just started learning Go and ran into a problem that I can't solve for several hours. I need to make a GET request to an external API that returns JSON, and parse this response into a struct.
Here's what I tried:
```go
package main
import (
"fmt"
"net/http"
)
func main() {
resp, err := http.Get("https://api.example.com/data")
if err != nil {
fmt.Println("Error:", err)
}
defer resp.Body.Close()
fmt.Println(resp) // What next?
}
```
The problem is that `resp` is not the JSON itself, but some kind of Response object. I don't understand how to extract data from it into a struct. I tried reading the documentation, but there's a lot about `io.Reader` and `json.Decoder`, and I got confused.
Specific questions:
1. How to properly read the response body