Since this question was asked and answered, the bufio package was updated (for Go 1.1), and perhaps a more pleasant solution is now available (not that it was all bad).
The Scanner type from the bufio package makes this very simple:
func main() { f, e := os.Open("one-hundred_50.txt") if e != nil { // error opening file, handle it } s := bufio.NewScanner(f) for s.Scan() { // scanner.Text() contains the current line } if e = s.Err(); e != nil { // error while scanning; no error at EOF } }
burfl source share