On Mon, Aug 24, 2015 at 1:21 PM, wrote:

Thanks. There are 3 common cases I've encountered where the io.Copy is
- 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 body

Deferring 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
All good points.

A couple counterpoints to using this universally:

- You may not want to wait for the full response, and discarding the
connection is better or faster for various reasons. You could dispatch it
in another goroutine, but then you need to move the Close to that
goroutine, it adds a little more overhead, and wastes bandwidth for large

- You need to watch out for unexpectedly large responses, though you can
mitigate this somewhat by wrapping the resp.Body in an io.LimitedReader.

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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 10 of 11 | next ›
Discussion Overview
groupgolang-nuts @
postedDec 10, '14 at 1:46p
activeAug 24, '15 at 6:41p



site design / logo © 2021 Grokbase