Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Go

Idiom #101 Load from HTTP GET request into a string

Make an HTTP request with method GET to the URL u, then store the body of the response in the string s.

import "io"
import "net/http"
res, err := http.Get(u)
if err != nil {
	return err
}
buffer, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
	return err
}
s := string(buffer)

res has type *http.Response.
buffer has type []byte.
It is idiomatic and strongly recommended to check errors at each step.
(def s (slurp u))

New implementation...