FAQ
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.
On Wednesday, December 10, 2014 at 12:17:26 PM UTC-8, Dave Cheney wrote:

I am sorry. This logic has changed in the past a few times and I am out of
date.

I think the current logic is

io.Copy(ioutil.Discard, resp.Body); resp.Body.Close() // connection
will be reused
resp.Body.Close() // remaining body will be discarded, and the
connection will _not_ be reused.

On Thu, Dec 11, 2014 at 1:39 AM, James Bardin <j.ba...@gmail.com
<javascript:>> wrote:
On Wed, Dec 10, 2014 at 9:34 AM, Chandru <chand...@gmail.com
<javascript:>> wrote:
Am I reading it wrong?

No, you are correct. Some of the body may be read into a buffer, but there's
no guarantee how much.
--
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

Previous

Follow ups

Related Discussions

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

People

Translate

site design / logo © 2021 Grokbase