Go标准库之io 发表于 2020-04-27 常量1234io.EOF 根据reader接口说明,在n>0且数据被读完的情况下,当次返回的error有可能是EOF也有可能是nilSeekStart = 0 // seek relative to the origin of the fileSeekCurrent = 1 // seek relative to the current offsetSeekEnd = 2 // seek relative to the end 接口123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263type Reader interface {Read(p []byte) (n int, err error)}对于一个可读对象通常需要实现Reader接口type Writer interface {Write(p []byte) (n int, err error)}可写type ReaderAt interface {ReadAt(p []byte, off int64) (n int, err error)}从off处开始读len(p)个字节type WriterAt interface {WriteAt(p []byte, off int64) (n int, err error)}从off处开始写len(p)个字节type ReaderFrom interface {ReadFrom(r Reader) (n int64, err error)}type WriterTo interface {WriteTo(w Writer) (n int64, err error)}结合ReaderFrom和WriterTo 我们可以做到"一次性从某个地方读取或写到某个地方去"type Seeker interface {Seek(offset int64, whence int) (ret int64, err error)}Seeker设置下次write或read的偏移量为offset,whence:0标识相对文件起点处,1标识相对文件当前偏移量,2现对于文件结尾处,Seek返回新的偏移量和错误type Closer interface {Close() error}用于关闭数据流type ByteReader interface {ReadByte() (c byte, err error)}type ByteWriter interface {WriteByte(c byte) error}按字节读取或写入,这两个接口在二进制数据或归档压缩时用的比较多ByteScannerRuneReaderRuneScanner组合接口 ReadCloser ReadSeeker ReadWriteCloser ReadWriteSeeker ReadWriter WriteCloser WriteSeeker