Scanner ErrTooLong Raises Token Limit

Hit bufio.ErrTooLong on a line past the 64KiB default, then raise the cap with Scanner.Buffer so oversized records still parse.

GoData Integrity

Code

gosnippet
29 lines
package main

import (
	"bufio"
	"fmt"
	"strings"
)

func main() {
	longLine := strings.Repeat("x", 70000)
	input := longLine + "
short line
"

	// the default token cap is 64KiB: the long line stops the scan with bufio.ErrTooLong
	scanner := bufio.NewScanner(strings.NewReader(input))
	for scanner.Scan() {
		fmt.Println("token len:", len(scanner.Bytes()))
	}
	fmt.Println("first pass:", scanner.Err())

	// Buffer raises the token limit: initial size, then maximum
	scanner = bufio.NewScanner(strings.NewReader(input))
	scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
	for scanner.Scan() {
		fmt.Println("token len:", len(scanner.Bytes()))
	}
	fmt.Println("second pass:", scanner.Err())
}

Inside the snippet

Scanner ErrTooLong Raises Token Limit comes down to the function main.

The file repeats itself only inside for scanner.Scan().

Watch out for

  • No lockfile entry, no version pinning — the file needs nothing beyond the runtime.

Putting it to use

The run emits 3 labeled sections — token len:, first pass:, second pass: — one per step.

Core lines

scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)

Interactive Tools for This Topic

FAQ

What does Scanner ErrTooLong Raises Token Limit print when run?
Invoke the file as-is — the `token len:`, `first pass:`, `second pass:` prefixes identify which step printed each line.