Seek() 用法
func (f *File) Seek(offset int64, whence int) (ret int64, err error)
file, _ := os.Open("./main.go")
buf := make([]byte, 100)
// whence 0:文件的原点(初始位置) 1:当前偏移量(ret的位置) 2:文件结束位置
fmt.Println(file.Seek(0, 2)) // 985 <nil> ==> 当前位置重置为文件结束位置,ret代表 file.Read 时的起始位置(相对于整个文件开始,偏移了多少字节),当前偏移量。从这里可以知道 ./main.go 文件大小为 985 字节
fmt.Println(file.Read(buf)) // 0 <nil> ==> 所以这里读取是空的。因为此时当前位置是文件结尾处
fmt.Println(file.Seek(-10, 1)) // 978 <nil> ==> 从当前位置往前偏移10个字节
fmt.Println(file.Read(buf)) // 10 <nil> ==> 所以从当前位置开始读只有10个字节
fmt.Println(file.Seek(0, 0)) // 0 <nil> ==> 当前位置重置为文件起始位置
fmt.Println(file.Read(buf)) // 100 <nil> ==> 从开始位置读,可以读满整个 buf