为什么golang中的响应体是readCloser?
我想知道 golang 中的 http 包是如何工作的。我可以看到http响应的主体是这样的:
type Response struct {
StatusCode int
Header Header
Body io.ReadCloser
}
Body
是一个ReadCloser。这是为什么?
主要问题是:http包是否同时完整地读取 header 和 body,然后返回 Response 或它只读取 Header 部分,当我们读取时Body
我们实际上是从连接中读取内容吗?我们是否有可能在完成整个正文之前遇到错误(例如因为连接断开)?或者当我们收到响应时,正文已完全接收并驻留在内存中?
I'm wondering how http package in golang works. I can see that the body of http response is like this:
type Response struct {
StatusCode int
Header Header
Body io.ReadCloser
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您可以从中读取内容,并且完成后必须关闭它。
否。
是的
是的。
不。想想 4.5 TByte 的流。
Because you can Read from it and you have to Close it once you are done.
No.
Yes
Yes.
No. Think of a 4.5 TByte stream.