Skip to content

Instantly share code, notes, and snippets.

@crowdmatt
Last active December 14, 2015 07:29
Show Gist options
  • Save crowdmatt/5051453 to your computer and use it in GitHub Desktop.
Save crowdmatt/5051453 to your computer and use it in GitHub Desktop.
The purpose of this function is to be as memory efficient as possible when operating on files with many, many lines, in Golang
import (
"bufio"
"os"
)
// readWholeLine returns a single line (without the ending \n) from the input buffered reader.
// An error is returned iff there is an error with the buffered reader.
func readWholeLine(r *bufio.Reader) (string, error) {
var (isPrefix bool = true
err error = nil
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln),err
}
// ForeachLine opens the file specified in filename, and reads each line until the \n, one at a time.
// It then invokes the callback method (called withLine) with the string gotten from that line.
// The purpose of this function is to be as memory efficient as possible when operating on files with many, many lines.
//
// Example:
// ForeachLine("/Users/myuser/Desktop/longlonglong.log", func(line string) {
// exampleFunction(line) // ... do whatever I need to with line
// })
func ForeachLine(filename string, withLine func(string)) {
f, err := os.Open(filename)
if err != nil { log.Fatal(err) }
r := bufio.NewReader(f)
s, e := readWholeLine(r)
for e == nil {
withLine(s)
s,e = readWholeLine(r)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment