GoLang io
常量
io 包中,只定义了三个常量,作为Seek 方法的 whence 参数的值,用于文件或其他可定位数据流的 寻址 操作。
// Seek whence values.
const (
// 从数据流起始位置开始计算偏移量
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
)
io.Seeker 接口
type Seeker interface {
Seek(offset int64, whence int) (int64, error)
}
Seek 方法用于设置下一次读写操作的偏移量
func (o *OffsetWriter) Seek(offset int64, whence int) (int64, error) {
switch whence {
default:
return 0, errWhence
case SeekStart:
offset += o.base
case SeekCurrent:
offset += o.off
}
if offset < o.base {
return 0, errOffset
}
o.off = offset
return offset - o.base, nil
}
SeekStart
从数据流起始位置开始计算偏移量
SeekCurrent
从数据流当前读写位置开始计算偏移量 SeekCurrent = 1 // seek relative t
SeekEnd
从数据流的末尾开始计算偏移量
变量
EOF
EOF 是当没有更多输入可用时 Read 返回的错误。 函数应当仅在表示输入正常结束时返回 EOF。
// EOF is the error returned by Read when no more input is available.
// (Read must return EOF itself, not an error wrapping EOF,
// because callers will test for EOF using ==.)
// Functions should return EOF only to signal a graceful end of input.
// If the EOF occurs unexpectedly in a structured data stream,
// the appropriate error is either [ErrUnexpectedEOF] or some other error
// giving more detail.
var EOF = errors.New("EOF")
ErrClosedPipe
ErrClosedPipe 用于对已关闭管道进行读写操作时返回的错误。
// ErrClosedPipe is the error used for read or write operations on a closed pipe.
var ErrClosedPipe = errors.New("io: read/write on closed pipe")
ErrNoProgress
当 Reader 客户端多次调用 Read 方法都未能返回数据或错误时,会返回 ErrNoProgress
// ErrNoProgress is returned by some clients of a [Reader] when
// many calls to Read have failed to return any data or error,
// usually the sign of a broken [Reader] implementation.
var ErrNoProgress = errors.New("multiple Read calls return no data or error")