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
+21.2k Golang : Convert Unix timestamp to UTC timestamp
+5.3k Apt-get to install and uninstall Golang
+15.5k Golang : How to generate QR codes?
+16.4k Convert JSON to CSV in Golang
+22.2k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+4.1k Golang : micron to centimeter example
+4.2k Golang : Get FX sentiment from website example
+16.1k Golang : delete and modify XML file content
+7.7k Golang : Accept any number of function arguments with three dots(...)
+7.9k Golang : How to use Gorilla webtoolkit context package properly
+6.8k Swift : Convert (cast) String to Double
+14.2k Golang : Get checkbox or extract multipart form data value example