Response Body and Connection Reuse in Go
Introduction If you have ever used the net/http package in Go, you know that you should close Response.Body. The io.ReadCloser type and its accompanying comment make this very clear. type Response struct { ... // 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. The default HTTP client's Transport may not // reuse HTTP/1.x "keep-alive" TCP connections if the Body is // not read to completion and closed. // // The Body is automatically dechunked if the server replied // with a "chunked" Transfer-Encoding. // ... Body io.ReadCloser ... } If you are attentive, you might also notice the part about reusing the connection. The comment states that for the underlying connection to be reused, the Body should be read to completion. However, for me, itβs unclear whoβs responsible for that. Why? I am not exactly sure - maybe because Iβve gotten used to Close methods handling all termination logic, or because the comment on the next line looks like a good-to-know detail rather than a call to action. ...