1

求教 go 读取文件并记录偏移量的正确姿势

 2 years ago
source link: https://www.v2ex.com/t/844095
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Go 编程语言

求教 go 读取文件并记录偏移量的正确姿势

  dongtingyue · 13 小时 19 分钟前 · 319 次点击
f.Seek(size,0)
bfRd := bufio.NewReader(f)
line, _, err := bfRd.ReadLine()
offset +=len(string(line))+2
...省略

文件内容这样

aaaaa
bbbbb
cccc

程序计算的偏移量会大于实际文件 2 个字节,因此只能读取结束后减 2 。有其他更简洁的方式么?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK