csv

package
v0.27.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package csv wraps encoding/csv as a codec.Codec implementation. The CSV format serialises a matrix of strings; the codec accepts [][]string for Marshal and writes into *[][]string on Unmarshal.

A slice of structs is a table too (#902): Marshal writes a header from the columns its fields map — a csv:"name" tag, else the Go name; "-" skips a field, ",optional" lets a file lack its column — then one record per row, and Unmarshal into a pointer to such a slice reads the header and converts each cell to its field: strings, integers, floats, bools, and any type that reads and writes itself as text (time.Time in RFC 3339). Unknown columns are ignored. It is a codec.StreamingCodec: one record per Decode and Encode, a []string or a struct.

Index

Constants

This section is empty.

Variables

View Source
var (
	//: Codec is the CSV singleton, registered with core/data/codec at package
	//: load. Binding the registration result to a named var is more
	//: idiomatic than `var _ = codec.Register(...)` and keeps us clear of
	//: init(). The singleton has escapeFormulas=false for wire-format
	//: fidelity — consumers whose output is read by a spreadsheet
	//: application should use NewWithEscape(true) instead.
	Codec codec.Codec = codec.Register(&csvCodec{})
)

Package-level vars: the Marshal-buffer pool plus the singleton registered with core/data/codec at package load.

Functions

func New

func New() codec.Codec

New returns the CSV singleton with escapeFormulas disabled. This preserves the v1 API and keeps the wire-format lossless for pipe-to-pipe use.

func NewWithEscape

func NewWithEscape(escape bool) codec.Codec

NewWithEscape returns a fresh CSV codec with the formula-injection mitigation toggled on or off. Use this constructor when the encoded output will be opened in a spreadsheet app (Excel, LibreOffice, Google Sheets): without the mitigation an attacker-influenced cell starting with "=" evaluates as a formula at open time, which is OWASP CSV Injection (CWE-1236). The returned codec is not registered with the core/data/codec singleton registry.

func NewWithOptions added in v0.27.0

func NewWithOptions(o Options) (c codec.Codec, err error)

NewWithOptions returns a fresh CSV codec configured by o — the delimiter, the formula mitigation, and whether a table of structs has a header row. A delimiter encoding/csv refuses is OPTIONS_INVALID. The returned codec is not registered: Format("csv") keeps resolving to the default singleton.

Types

type Options added in v0.27.0

type Options struct {
	// Escape turns on the OWASP CSV-injection mitigation (CWE-1236) when
	// writing: a cell starting with '=', '+', '-', '@', TAB or CR is prefixed
	// with a single quote so a spreadsheet shows it as text. See NewWithEscape.
	Escape bool
	// Comma is the field delimiter; zero means ','. It must be a valid rune
	// other than '"', '\r', '\n' and U+FFFD, as encoding/csv requires.
	Comma rune
	// NoHeader writes and reads a table of structs without a header row: the
	// columns are the struct's fields, in order. It does not change a
	// [][]string, whose records are all data.
	NoHeader bool
}

Options configure a CSV codec built by NewWithOptions. The zero Options is the registered codec's behaviour.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL