Why does the io.WriterTo WriteTo method return int64 rather than int?

Most output methods in Go io package return (int, error) , for example, io.Writer Write([]byte) and the io.WriteString(io.Writer, string) function. However, some of the output methods, such as io.WriterTo WriteTo , return (int64, error) . This makes it inconvenient to implement WriteTo in terms of Write or WriteString without saving the intermediate value and the type that converts it from int to int64 . What is the reason for this discrepancy?

+6
source share
2 answers

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

With the io.Reader and io.Writer amount of data is limited by the size of the given slice, the length of which is limited int for the current architecture.

+6
source

The signature of the Writer.Write() method:

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

It records the contents of the fragment. Quoting from spec: Types of slices :

A slice is a descriptor for an adjacent segment of a base array ...

As we all know, a slice has a base array. Quoting again from Spec: Array Types :

Length is part of an array; it must evaluate the non-negative constant represented by an int value.

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

So, back to the Writer.Write() method: since it writes the contents of the transmitted slice, it is guaranteed that the number of bytes written will be no more than what fits in the int .

Now the WriteTo.WriteTo() method:

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

Breaking or array is not mentioned anywhere. You have no guarantee that the result will match int , so int64 more than justified.

Example: BigBuffer

Imagine a BigBuffer implementation that temporarily writes data to an array or slice. An implementation can manage multiple arrays, so if one is full (for example, max int is reached), it continues in the other. Now, if this BigBuffer implements the WriteTo interface, and you call this method to write the contents to os.File , the result will be greater than max int .

+4
source

Source: https://habr.com/ru/post/985211/


All Articles