Read an HTTP response body in Go using io.ReadAll on resp.Body and ensure you defer resp.Body.Close().
Read the HTTP response body in Go by calling io.ReadAll on the resp.Body field returned by http.Get or http.Client.Do.
resp, err := http.Get("https://example.com")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
Reading an HTTP response body in Go lets you grab the actual content sent back by a website after you ask for it. Think of it like opening a letter; you first get the envelope (the response), then you open it to read the paper inside (the body). You must always close the connection afterward to free up resources.