package main import ( "fmt" "net/http" "io/ioutil" ) func main() { res, err := http.Get("https://www.example.com") if err != nil { panic(err) } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { panic(err) } fmt.Println("Content-Length:", res.ContentLength) fmt.Println("Response Body:", string(body)) }
package main import ( "fmt" "net/http" "io/ioutil" ) const MaxResponseSize = 10 * 1024 * 1024 // max response size in bytes func main() { res, err := http.Get("https://www.example.com") if err != nil { panic(err) } defer res.Body.Close() // check if response exceeds max size if res.ContentLength != -1 && res.ContentLength > MaxResponseSize { panic("response size exceeds max size limit") } body, err := ioutil.ReadAll(res.Body) if err != nil { panic(err) } fmt.Println("Response Body:", string(body)) }Both examples make use of the `net/http` package to make an HTTP GET request to a URL and retrieve the response. The first example simply prints the Content-Length header and response body, while the second example checks if the response exceeds a maximum size limit before reading the response body.