On Tue, Jun 22, 2021 at 4:31 PM 'Dan Kortschak' wrote:
> Yes, trivial examples also exist.

I thought one possible reason was `http#Response.Body` [1], because if
a response contains `Content-Encoding: gzip`, then Go will
automatically wrap the Body in a `gzip.Reader`. If that was the case,
then a Close method would be required, as Body being a
`io.ReadCloser`, it must have a `Read` and `Close` method. However
`gzip#Reader.Close` specifically says that it doesnt close the
underlying reader:

> Close closes the Reader. It does not close the underlying io.Reader.

So wrapping Body with gzip Reader directly would be bad, because the
Body would be left open on Close. The source code confirms that they
use a custom gzip type instead [2]. Interestingly, the Close method on
this type [3] closes the underlying reader, but doesnt bother to close
the gzip reader, even using `defer`. So I think it can be safe to omit
using gzip Reader Close in the general case.

1. https://golang.org/pkg/net/http#Response.Body
2. https://github.com/golang/go/blob/go1.16.5/src/net/http/transport.go#L2187
3. 
https://github.com/golang/go/blob/go1.16.5/src/net/http/transport.go#L2831-L2833

-- 
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/CAP8dQmsEs5s9QHiiHXc-44jQPz0sztazm1ScUO1yh2cOjurOxA%40mail.gmail.com.

Reply via email to