英文:
Why does ReadRequest use bufio.Reader?
问题
方法http.ReadRequest
接受bufio.Reader
而不是io.Reader
作为参数。这背后的原因是什么?
英文:
The method http.ReadRequest
takes a bufio.Reader
instead of an io.Reader
. What is the reason behind this?
答案1
得分: 0
它无论如何都会使用缓冲读取器(buffered reader)。但是,它不会自动将您的io.Reader
包装到缓冲区中,因为bufio.Reader
的特性是可以从底层的io.Reader
中获取更多数据,而算法实际上可能不需要那么多数据。而且,由于底层的io.Reader
无法将多余的数据推回,这些数据将保留在缓冲区中。如果自动包装,那么在请求被读取后,缓冲区将丢失,并且您的io.Reader
将变得损坏。因此,它要求您明确地包装您的读取器以防止数据丢失。
英文:
It will use buffered reader anyway. But it will not automatically wrap your io.Reader
into buffer because of bufio.Reader
nature - it can grab more data from underlying io.Reader
that algorithm actually need. And, since underlying io.Reader
cannot push back that exceed data, it will remain in that buffer. In case of autowrap that buffer would be lost after request was read and your io.Reader
become corrupted. Thus, it ask you to explicitly wrap your reader to prevent data loss.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论