golang教程|Golang中seek使用方法详解

更新时间:2020-09-20    来源:Google    手机版     字体:

【www.bbyears.com--Google】


习惯了php中的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) #跳转到当前位置(位置不变)

这样就很好理解了。

f,_:=os.Open("a.txt")
//从头开始,文件指针偏移100
f.Seek(100,0)
buffer:=make([]byte,1024)
// Read 后文件指针也会偏移
_,err:=f.Read(buffer)
if err!=nil{
    fmt.Println(nil)
    return
}
// 获取文件指针当前位置
cur_offset,_:=f.Seek(0,os.SEEK_CUR)
fmt.Printf("current offset is %d\n", cur_offset)

本文来源:http://www.bbyears.com/seo/99803.html

热门标签

更多>>

本类排行