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

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

  •  
  •   dongtingyue · Mar 31, 2022 · 1948 views
    This topic created in 1514 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
        1
    shadeofgod  
       Mar 31, 2022
    看文档,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
        2
    dongtingyue  
    OP
       Mar 31, 2022
    JKeita
        3
    JKeita  
       Mar 31, 2022
    这 2 字节是换行吧?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   961 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 216ms · UTC 19:24 · PVG 03:24 · LAX 12:24 · JFK 15:24
    ♥ Do have faith in what you're doing.