golang_file_tell

习惯了python中的seek和tell,转到golang时突然发现只有Seek发现,tell方法不见了。google了一下,发现了tell的实现方法:
File.Seek(0, os.SEEK_CUR) 或者File.Seek(0,1) 参考

解释:
先来看下Seek方法
func (f *File) Seek(offset int64, whence int) (ret int64, err error)
跳转到文本中的某处,并返回此处的偏移量
File.Seek(0, os.SEEK_CUR) #跳转到当前位置(位置不变)
这样就很好理解了。

1
2
3
4
5
6
7
8
9
10
f,_:=os.Open("a.txt")
f.Seek(100,0) //从头开始,偏移100
buffer:=make([]byte,1024)
_,err:=f.Read(buffer)
if err!=nil{
fmt.Println(nil)
return
}
cur_offset,_:=f.Seek(0,1)
fmt.Printf('current offset is %d\n', cur_offset)