为什么io.WriterTo的WriteTo方法返回int64而不是int? [英] Why does io.WriterTo's WriteTo method return an int64 rather than an int?
问题描述
Go的io
包中的大多数输出方法都返回(int, error)
,例如io.Writer
的Write([]byte)
方法和io.WriteString(io.Writer, string)
函数.但是,某些输出方法(例如io.WriterTo
的WriteTo
方法)将返回(int64, error)
.这使得根据Write
或WriteString
来实现WriteTo
而不存储中间值并将其从int
转换为int64
的类型非常不便.这种差异的原因是什么?
Most of the output methods in Go's io
package return (int, error)
, for example io.Writer
's Write([]byte)
method and the io.WriteString(io.Writer, string)
function. However, a few of the output methods, such as io.WriterTo
's WriteTo
method, return (int64, error)
instead. This makes it inconvenient to implement WriteTo
in terms of Write
or WriteString
without storing an intermediate value and type converting it from int
to int64
. What is the reason for this discrepancy?
推荐答案
WriteTo
可能复制了超过int32字节的数据.
It's possible that WriteTo
copies more than int32 bytes of data.
使用io.Reader
和io.Writer
接口,数据量受给定切片的大小限制,对于当前体系结构,切片的长度受int
限制.
With the io.Reader
and io.Writer
interfaces, the amount of data is limited by the size of the given slice, which has a length limited by int
for the current architecture.
这篇关于为什么io.WriterTo的WriteTo方法返回int64而不是int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!