为什么 golang 中的 response body 是 readCloser?

Why is response body in golang is a readCloser?

我想知道 golang 中的 http 包是如何工作的。我可以看到http响应的body是这样的:

type Response struct {
    StatusCode int
    Header     Header
    Body       io.ReadCloser
}

Body 是一个 ReadCloser。这是为什么?

主要问题是:http 包是同时完整读取 header 和 body 然后 returns Response 还是只读取 Header 部分,当我们从 Body 读取时,我们实际上是从连接读取?我们是否有可能在完成整个 body 之前遇到错误(例如因为连接断开)?或者当我们得到响应时 body 被完全接收并驻留在内存中?

The Body is a ReadCloser. Why is that?

因为您可以从中读取并且完成后必须关闭它。

The main question is: Does http package read header and body at the same time and completely and then returns the Response [?]

没有

or it just reads the Header part and when we are reading from Body we are actually reading from a connection?

Is it possible that we encounter an error before we finish the whole body(e.g because connection drops)?

是的。

or the body is completely received and resides in the memory when we get the response?

没有。想想一个 4.5 TB 的流。