Yes, and if you don't resp.Body.Close() will do it for you.
Yes, and if you don't resp.Body.Close() will do it for you.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Is it considered a best practice to always add the following after http.DefaultClient.Do returns a non-error response?defer res.Body.Close()defer io.Copy(ioutil.Discard, res.Body)That way subsequent code in the function can read the body however it sees fit, but if it doesn't read it to completion for some reason, we cleanup and make sure the connection can be re-used. And it's a no-op if the body was already read fully.
Thanks. There are 3 common cases I've encountered where the io.Copy is needed:- Reading the body using json.Decoder - it can stop without reading the entire body contents- Early return without reading the body b/c of a non-200 response- Client code doing a POST where it checks the http response code without reading the bodyDeferring the io.Copy at the same spot as the Close lets me ensure the body is always read without having to manually review each error case