Is there a ReadLine equivalent for a file in Go? -


i want readbytes until "\n" text file, not bufio.

is there way without converting bufio?

there many ways it, wrapping bufio suggest. if doesn't work (why not?), can go ahead , read single bytes this:

full working example:

package main  import (     "bytes"     "fmt"     "io" )  // readline reads line delimited \n io.reader // unlike bufio, rather inefficiently reading 1 byte @ time func readline(r io.reader) (line []byte, err error) {     b := make([]byte, 1)     var l int     err == nil {         l, err = r.read(b)         if l > 0 {             if b[0] == '\n' {                 return             }             line = append(line, b...)         }     }     return }  var data = `hello, world! write 3 lines.`  func main() {      b := bytes.newbufferstring(data)      {         line, err := readline(b)         fmt.println("line: ", string(line))         if err != nil {             return         }     } } 

output:

line:  hello, world! line:  write line:  3 lines. 

playground: http://play.golang.org/p/dfb0ghppnm


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -