Hands-On System Programming with Go
上QQ阅读APP看书,第一时间看更新

Reader interface

For all operations that read from a disk, there's an interface that is paramount:

type Reader interface {
Read(p []byte) (n int, err error)
}

Its job is really simple – fill the given slice of bytes with the content that's been read and return the number of bytes that's been read and an error, if one occurs. There is a special error variable that's defined by the io package, called EOF (End Of File), which should be returned when there is no more input available.

A reader makes it possible to process data in chunks (the size is determined by the slice), and if the same slice is reused for the operations that follow, the resulting program is consistently more memory efficient because it is using the same limited part of the memory that allocates the slice.