Package bufio

Table of Contents

Read a file line by line howto

reader.ReadString('\n') isn't fully equivalent to ReadLine because ReadString is unable to handle the case when the last line of a file does not end with the newline character.

The upshot:

reader.ReadString('\n')
  • If you don't mind that the line could be very long (i.e. use a lot of RAM)
  • It keeps the \n at the end of the string returned.
reader.ReadLine()
  • If you care about limiting RAM consumption
  • If you don't mind the extra work of handling the case where the line is greater than the reader's buffer size.
  • For reader, the defaultBufSize is 4096 bytes.
scanner.Scan(); scanner.Text()
  • It does not handle long lines. (Just returns ErrTooLong)
  • For scanner, the MaxScanTokenSize is 64 * 1024 bytes.