英文:
Whats the proper way to copy data from an io.Reader directly to a destination bytes.Buffer in golang?
问题
我有一段代码,想要将数据从 io.Reader 直接复制到一个 bytes.Buffer 结构体中,该结构体旨在作为缓存一直保存在内存中。目前我只是调用 io.Copy(dstBytesBuffer, reader)
。但是查看 io.Copy 的代码,它似乎是在创建一个缓冲区,并将数据从 reader 复制到该缓冲区,然后再从该缓冲区写入到我的 dstBytesBuffer 中。有没有办法跳过这一步,直接从 reader 复制到我的目标缓冲区?
英文:
I have code that wants to copy data from an io.Reader directly to a bytes.Buffer struct which is intended to stay around in memory as a cache. Right now I'm just calling io.Copy(dstBytesBuffer, reader)
. But looking at the io.Copy code it looks like it is creating a buffer itself and copying data from the reader into this buffer, and then writing from that buffer to my dstBytesBuffer. Is there a way to skip that and just have it copy straight from reader to my destination buffer?
答案1
得分: 3
使用io.Copy()
是完全可以的。io.Copy()
针对多种用例进行了优化。引用它的文档:
Copy从src复制到dst,直到在src上达到EOF或发生错误。它返回复制的字节数和在复制过程中遇到的第一个错误(如果有)。
成功的Copy返回err == nil,而不是err == EOF。因为Copy被定义为从src读取直到EOF,所以它不将来自Read的EOF视为要报告的错误。
如果src实现了WriterTo接口,则通过调用src.WriteTo(dst)来实现复制。否则,如果dst实现了ReaderFrom接口,则通过调用dst.ReadFrom(src)来实现复制。
即使不知道你的源io.Reader
的任何信息,即使它没有提供高效的WriteTo(dst)
方法,我们肯定知道你的目标是一个bytes.Buffer
,它实现了io.ReaderFrom
,因为它有一个Buffer.ReadFrom()
方法,该方法从给定的io.Reader
读取,而不创建或使用额外的缓冲区。
英文:
Using io.Copy()
for this is perfectly fine. io.Copy()
is "optimized" for several use cases. Quoting from its doc:
> Copy copies from src to dst until either EOF is reached on src or an error occurs. It returns the number of bytes copied and the first error encountered while copying, if any.
>
> A successful Copy returns err == nil, not err == EOF. Because Copy is defined to read from src until EOF, it does not treat an EOF from Read as an error to be reported.
>
> If src implements the WriterTo interface, the copy is implemented by calling src.WriteTo(dst). Otherwise, if dst implements the ReaderFrom interface, the copy is implemented by calling dst.ReadFrom(src).
Without knowing anything from your source io.Reader
, even if it doesn't provide an efficient WriteTo(dst)
method, we surely know your destination is a bytes.Buffer
and it implements io.ReaderFrom
as it has a Buffer.ReadFrom()
method which reads from the given io.Reader
without creating or using additional buffers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论