To be clear, I said "things I could imagine". But:

On Mon, Jun 21, 2021 at 11:28 PM Steven Penny <srp...@gmail.com> wrote:

> Thanks for the response. Couple of points:
>
> > 1. You don't get notified about checksum mismatches or the like. i.e. the
> > data you read might be corrupted and you might not realize it.
>
> I dont think this is true. I tried with this file [1], and Close returns
> the
> same error as, for example io.Copy, so again Close seems redundant.
>

That doesn't seem like a particularly conclusive test. There are a couple
of layers of readers and I would assume some buffering is going on. So if
there is indeed a checksum mismatch reported by `Close` but not a previous
`Read`, it might depend on details like alignment of blocks or specific
gzip parameters used in the compressed file. Saying "on this particular
file, `Close` returned the same error" is just not meaningful.

That being said: Again, I was just guessing at potential issues. If I was
sufficiently motivated, I would look at the code and see under what
circumstances `Close()` would return an error - I tried, but it quickly
became non-trivial enough for me to lose motivation.


> > Either way, it's also just good hygiene to *always* call `Close` on an
> > `io.Closer`. If a library gives you a `Close` method, it usually expects
> you
> > to call it and there's really no point in not doing it.
>
> Even with something like net/http#Head? Seems pointless in that case too.
>

Yes, even in that case and I'm not sure it is pointless. I would assume
that the client code does not special case `HEAD` requests and just uses
the same logic when returning the response as if it was a `GET` request -
in which case you have to `Close` the body to make sure a Keep-Alive
connection can be re-used (I think. Fuzzy on the details). Again, if I was
sufficiently motivated, I would check.

But the docs seem very clear to me:

// The http Client and Transport guarantee that Body is always
> // non-nil, even on responses without a body or responses with
> // a zero-length body. It is the caller's responsibility to
> // close Body.


To me, this reads as clearly documenting that you have to close the body,
even on responses without a body (like HEAD).

Again, the idiom is, if you get an `io.Closer`, `Close` should be called
once you're done with it.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfE4nYngifjSQK9OsQYhqegRTpF-X4UFc728KVH59CLtww%40mail.gmail.com.

Reply via email to