io.WriterTo的WriteTo方法为什么返回int64而不是int?

huangapple go评论98阅读模式
英文:

Why does io.WriterTo's WriteTo method return an int64 rather than an int?

问题

Go的io中的大多数输出方法返回(int, error),例如io.WriterWrite([]byte)方法和io.WriteString(io.Writer, string)函数。然而,少数输出方法(例如io.WriterToWriteTo方法)返回(int64, error)。这使得在不存储中间值并将其从int转换为int64的情况下,使用WriteWriteString来实现WriteTo变得不方便。这种差异的原因是什么?

英文:

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?

答案1

得分: 6

可能会复制超过int32字节的数据。

使用io.Readerio.Writer接口,数据的量受给定切片的大小限制,该切片的长度由当前架构的int限制。

英文:

It's possible that WriteTo copies more than int32 bytes of data.

With the io.Readerand 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.

答案2

得分: 5

Writer.Write() 方法的签名如下:

Write(p []byte) (n int, err error)

它将一个切片的内容写入。引用自规范:切片类型

> 切片是对底层数组的连续片段的描述符...

众所周知,切片有一个底层数组。再次引用自规范:数组类型

> 长度是数组类型的一部分;它必须是一个非负常量,可以由 int 类型的值表示。

因此,数组的最大长度受到 int 类型的最大值的限制(32位架构下为 2147483647,64位架构下为 9223372036854775807)。

所以回到 Writer.Write() 方法:由于它写入传递的切片的内容,可以保证写入的字节数不会超过 int 类型所能容纳的范围。

现在看看 WriteTo.WriteTo() 方法:

WriteTo(w Writer) (n int64, err error)

这里没有提到切片或数组。你无法保证结果能够适应 int 类型,因此使用 int64 是完全合理的。

示例:BigBuffer

想象一个 BigBuffer 的实现,它将数据临时写入一个数组或切片。该实现可以管理多个数组,以便当一个数组已满(例如达到最大的 int 值)时,继续写入另一个数组。现在,如果这个 BigBuffer 实现了 WriteTo 接口,并且你调用该方法将内容写入一个 os.File,结果将超过 int 的最大值。

英文:

The signature of the Writer.Write() method:

Write(p []byte) (n int, err error)

It writes the contents of a slice. Quoting from the spec: Slice types:

> A slice is a descriptor for a contiguous segment of an underlying array...

As we all know, the slice has an underlying array. Quoting again from the Spec: Array types:

> The length is part of the array's type; it must evaluate to a non-negative constant representable by a value of type int.

So the maximum length of an array is limited by the maximum value of the int type (which is 2147483647 in case of 32 bit and 9223372036854775807 in case of 64 bit architectures).

So back to the Writer.Write() method: since it writes the content of the passed slice, it is guaranteed that the number of written bytes will not be more that what fits into an int.

Now WriteTo.WriteTo() method:

WriteTo(w Writer) (n int64, err error)

A slice or array is nowhere mentioned. You have no guarantees that the result will fit into an int, so the int64 is more than justified.

Example: BigBuffer

Imagine a BigBuffer implementation which temporarily writes data into an array or slice. The implementation may manage multiple arrays so that if one is full (e.g. reached max int), continues in another one. Now if this BigBuffer implements the WriteTo interface and you call this method to write the content into an os.File, the result will be more than max int.

huangapple
  • 本文由 发表于 2015年4月16日 03:16:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/29658892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定