上QQ阅读APP看书,第一时间看更新
Closer and seeker
There are two other interfaces that are related to readers: io.Closer and io.Seeker:
type Closer interface {
Close() error
}
type Seeker interface {
Seek(offset int64, whence int) (int64, error)
}
These are usually combined with io.Reader, and the resulting interfaces are as follows:
type ReadCloser interface {
Reader
Closer
}
type ReadSeeker interface {
Reader
Seeker
}
The Close method ensures that the resource gets released and avoids leaks, while the Seek method makes it possible to move the cursor of the current object (for example, a Writer) to the desired offset from the start/end of the file, or from its current position.
The os.File structure implements this method so that it satisfies all the listed interfaces. It is possible to close the file when the operations are concluded, or to move the current cursor around, depending on what you are trying to achieve.