The Go Programming Language
› http://golang.org/
› Go Playground
Go Projects
› Revel Web Framework
dongtingyue

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

  •  
  •   dongtingyue · Mar 31, 2022 · 2358 views
    This topic created in 1692 days ago, the information mentioned may be changed or developed.

    目前是

    f.Seek(size,0)
    bfRd := bufio.NewReader(f)
    line, _, err := bfRd.ReadLine()
    offset +=len(string(line))+2
    ...省略
    

    文件内容这样

    aaaaa
    bbbbb
    cccc
    

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

    shadeofgod
    shadeofgod
    Mar 31, 2022
    1
    看文档,ReadLine tries to return a single line, not including the end-of-line bytes.

    Most callers should use ReadBytes('\n') or ReadString('\n') instead or use a Scanner.
    dongtingyue
    dongtingyue
    OP
    Mar 31, 2022
    2
    JKeita
    JKeita
    Mar 31, 2022
    3
    这 2 字节是换行吧?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   930 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 20:45 · PVG 04:45 · LAX 12:45 · JFK 15:45
    ♥ Do have faith in what you're doing.