Go version

go version go1.24.2 darwin/amd64

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE='on'
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/USERNAME/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/USERNAME/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/lp/cvm95zh96bx9g3zl1qjh5tdw0000gn/T/go-build1086479209=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/Users/USERNAME/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/USERNAME/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/USERNAME/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.24.2'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

package main

import (
    "bytes"
    "encoding/csv"
    "strings"
)

func main() {
    buf := &bytes.Buffer{}
    wr := csv.NewWriter(buf)
    err := wr.WriteAll([][]string{
        {"a", "b", "c"},
        {"d", "e"},
    })

    if err != nil {
        panic(err) // <-- label A: expect error ErrFieldCount here (or no error on reader)
    }

    wr.Flush()

    println("write completed")

    rd := csv.NewReader(buf)
    records, err := rd.ReadAll()

    if err != nil {
        panic(err) // <-- label B: received ErrFieldCount here (expect no error or error on Writer.WriteAll)
    }

    for _, record := range records {
        println(strings.Join(record, ", "))
    }
}

https://go.dev/play/p/vy890hAeCtv

What did you see happen?

A ErrFieldCount error was returned from csv.Reader.ReadAll, but not from csv.Writer.WriteAll

What did you expect to see?

A ErrFieldCount returned from csv.Writer.WriteAll - OR no error from csv.Reader.ReadAll - either writer autofills blank fields (sketchy) - OR reader allows mismatched field counts

Comment From: dmitshur

CC @dsnet, @bradfitz, @rsc.

Comment From: seankhliao

the Reader.FieldsPerRecord controls validation of the number of records.

I believe this is working as intended.

Comment From: gabyhelp

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

Comment From: ntbosscher

@seankhliao thank you, I must have missed that. Sorry about that!