Last active
December 14, 2015 07:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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