Golang : Read large file with bufio.Scanner cause token too long error
There are couple of ways to read files with Golang and one of the common way to read in file is with bufio.NewScanner() function. For example :
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
file, err := os.Open("somefile.dat")
if err != nil {
panic(err.Error())
}
defer file.Close()
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanBytes)
for scanner.Scan() {
fmt.Println(scanner.Bytes())
}
if err := scanner.Err(); err != nil {
fmt.Println("error while reading :", err)
}
}
Most of the time, things should be fine and dandy. However, there will be cases when the program attempts to read in a very large file and it will throw out this error message :
"bufio.Scanner: token too long"
This error message was caused by the input token size that exceeded the bufio.MaxScanTokenSize
buffer size. The allocated size is MaxScanTokenSize = 64 * 1024
To read in large files, it is recommended to use bufio.Reader.ReadLine instead.
References :
See also : Golang : Scanf function weird error in Windows
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+11.4k Golang : Handle API query by curl with Gorilla Queries example
+7.4k Golang : Rot13 and Rot5 algorithms example
+10.3k Golang : cannot assign type int to value (type uint8) in range error
+5.7k Fix yum-complete-transaction error
+8.3k Golang: Prevent over writing file with md5 hash
+6.5k Golang : Spell checking with ispell example
+11.3k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+11.6k Golang : Calculations using complex numbers example
+12k Golang : Perform sanity checks on filename example
+8.7k Golang : On lambda, anonymous, inline functions and function literals
+6.8k Default cipher that OpenSSL used to encrypt a PEM file
+14.8k Golang : Submit web forms without browser by http.PostForm example