Go标准库之io

常量

1
2
3
4
io.EOF 根据reader接口说明,在n>0且数据被读完的情况下,当次返回的error有可能是EOF也有可能是nil
SeekStart = 0 // seek relative to the origin of the file
SeekCurrent = 1 // seek relative to the current offset
SeekEnd = 2 // seek relative to the end

接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
type 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
}
按字节读取或写入,这两个接口在二进制数据或归档压缩时用的比较多
ByteScanner
RuneReader
RuneScanner
组合接口
ReadCloser
ReadSeeker
ReadWriteCloser
ReadWriteSeeker
ReadWriter
WriteCloser
WriteSeeker