Reads a single line from a buffered reader. The line is read into the passed in buffer to minimize allocations. This is the preferred method for loading long lines which could be longer than the buffer size of bufio.Scanner.
(r *bufio.Reader, buf *bytes.Buffer)
| 76 | // method for loading long lines which could be longer than the buffer |
| 77 | // size of bufio.Scanner. |
| 78 | func readLine(r *bufio.Reader, buf *bytes.Buffer) error { |
| 79 | isPrefix := true |
| 80 | var err error |
| 81 | for isPrefix && err == nil { |
| 82 | var line []byte |
| 83 | // The returned line is an internal buffer in bufio and is only |
| 84 | // valid until the next call to ReadLine. It needs to be copied |
| 85 | // over to our own buffer. |
| 86 | line, isPrefix, err = r.ReadLine() |
| 87 | if err == nil { |
| 88 | buf.Write(line) |
| 89 | } |
| 90 | } |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | func main() { |
| 95 | flag.Parse() |