Skip to content
Go: Report the source position of an invalid CSV field

Go: Report the source position of an invalid CSV field

November 14, 2026

A CSV record can span physical lines, and a non-ASCII field can occupy several bytes per character. Reader.FieldPos reports the source line and byte column for a field in the most recently returned record, which helps locate validation errors.

Set up

Tested on Linux amd64 with Go 1.26.10. Use Bash with Go on your PATH, a text editor, and the mktemp, rm, and rmdir utilities. The example uses only the standard library. Keep the same terminal through cleanup, and stop if setup fails before saving the source.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as go-csv-fieldpos-error-location.go:

package main

import (
	"encoding/csv"
	"fmt"
	"io"
	"strconv"
	"strings"
)

func main() {
	reader := csv.NewReader(strings.NewReader("é,12\n\"two\nlines\",oops\n"))
	reader.FieldsPerRecord = 2
	count := 0
	for {
		record, err := reader.Read()
		if err == io.EOF {
			break
		}
		if err != nil {
			panic(err)
		}
		line, column := reader.FieldPos(1)
		quantity, err := strconv.Atoi(record[1])
		if err != nil {
			fmt.Printf("%q: invalid quantity at line=%d byte-column=%d\n", record[0], line, column)
		} else {
			fmt.Printf("%q: quantity=%d at line=%d byte-column=%d\n", record[0], quantity, line, column)
		}
		count++
	}
	if count != 2 {
		panic("record count")
	}
}

Run:

go run go-csv-fieldpos-error-location.go

Observed output:

"é": quantity=12 at line=1 byte-column=4
"two\nlines": invalid quantity at line=3 byte-column=8

Read the result

The first quantity starts after a two-byte UTF-8 character and a comma, so its byte column differs from a character-based column. The quoted name in the second record spans a newline; the quantity starts on the final physical line of that record.

Get the field position after reading the record and before reading the next one. The parser tracks the original source location even though the returned field values no longer include CSV delimiters and surrounding quotes.

Apply it carefully

Line and column numbers start at one, while the field index starts at zero. An out-of-range field index panics; the fixed field count and successful read make index one valid here. FieldPos points to the start of the field, not the exact character that made numeric conversion fail. CSV syntax errors need their own handling, and numeric acceptance rules such as nonnegative bounds remain separate. This example uses in-memory input and does not impose a record-size limit.

See the Go standard-library reference.

Clean up

Remove the saved source and the empty scratch directory. Go manages its build cache separately. If the directory is not empty, inspect its contents before removing anything else.

rm -- go-csv-fieldpos-error-location.go
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on