Documentation
¶
Index ¶
- Variables
- type Column
- func ColumnAny(v any) Column
- func ColumnBool(v bool) Column
- func ColumnBytes(b []byte) Column
- func ColumnFloat32(v float32) Column
- func ColumnFloat64(v float64) Column
- func ColumnInt(v int) Column
- func ColumnInt8(v int8) Column
- func ColumnInt16(v int16) Column
- func ColumnInt32(v int32) Column
- func ColumnInt64(v int64) Column
- func ColumnString(s string) Column
- func ColumnTime(t time.Time, layout string) Column
- func ColumnUint(v uint) Column
- func ColumnUint8(v uint8) Column
- func ColumnUint16(v uint16) Column
- func ColumnUint32(v uint32) Column
- func ColumnUint64(v uint64) Column
- func ColumnUintptr(v uintptr) Column
- type ColumnKind
- type Option
- type Reader
- type Record
- type Writer
Constants ¶
This section is empty.
Variables ¶
var ErrBareQuote = errors.New("bare \" in non-quoted field")
ErrBareQuote is returned when a bare '"' appears in a non-quoted field.
var ErrEmptyRecord = errors.New("zerocsv: empty record")
ErrEmptyRecord is returned by Write when no columns are provided.
var ErrFieldCount = errors.New("wrong number of fields")
ErrFieldCount is returned by Next or Write when a record's field count does not match the expected number of fields (see WithFieldsPerRecord). It is non-fatal, like encoding/csv: the record is still returned or written and reading or writing can continue.
var ErrInvalidDelim = errors.New("zerocsv: invalid field delimiter")
ErrInvalidDelim is returned when a delimiter that would corrupt the CSV structure is configured on a Writer or Reader.
var ErrQuote = errors.New("extraneous or missing \" in quoted-field")
ErrQuote is returned for an extraneous or missing '"' in a quoted field.
var ErrRecordTooLarge = errors.New("zerocsv: record larger than the maximum buffer size")
ErrRecordTooLarge is returned by Next when a record is larger than the maximum buffer size configured with WithMaxBuffer and therefore cannot be parsed in memory. Reading cannot continue past the record.
Functions ¶
This section is empty.
Types ¶
type Column ¶
type Column struct {
// contains filtered or unexported fields
}
Column is a tagged, value-typed CSV field. Pass it to Write by value; building and writing columns performs no heap allocation for the typed constructors. ColumnAny is the exception and may allocate.
func ColumnAny ¶
ColumnAny returns a Column containing v, rendered with fmt.Sprint when the record is written. Unlike the typed constructors, this may allocate.
func ColumnBool ¶
ColumnBool returns a Column containing v, written as "true" or "false".
func ColumnBytes ¶
ColumnBytes returns a Column containing b. The slice is written as-is, with no copy.
func ColumnFloat32 ¶
ColumnFloat32 returns a Column containing v.
func ColumnFloat64 ¶
ColumnFloat64 returns a Column containing v.
func ColumnTime ¶
ColumnTime returns a Column containing t, formatted with layout when the record is written.
func ColumnUintptr ¶
ColumnUintptr returns a Column containing v.
type Option ¶
type Option func(*options)
Option configures a Writer or Reader at construction time.
func WithCRLF ¶
func WithCRLF() Option
WithCRLF makes the Writer end each record with "\r\n" instead of "\n".
func WithDelimiter ¶
WithDelimiter sets the field delimiter, for example ',' for CSV, '\t' for TSV, or ';' for semicolon-separated values. The NUL byte, '"', '\r' and '\n' are rejected: an invalid delimiter marks a Writer or Reader as failed, and Next, Write, WriteAll, Flush and Error report the error.
func WithFieldsPerRecord ¶ added in v1.1.0
WithFieldsPerRecord sets the expected number of fields per record, applying to both the Reader and the Writer.
If n is positive, Next and Write require every record to have exactly n fields and return ErrFieldCount otherwise. If n is 0, the count is taken from the first record and enforced on all subsequent ones, like encoding/csv's default. If n is negative, no check is made and records may have a variable number of fields. Blank lines read by the Reader never take part in the check.
Like encoding/csv, ErrFieldCount is non-fatal: the mismatched record is still returned (Reader) or written (Writer), and reading or writing may continue.
func WithLazyQuotes ¶
func WithLazyQuotes() Option
WithLazyQuotes makes the Reader tolerate malformed quoting: a bare '"' in an unquoted field, or a non-doubled '"' in a quoted field, is treated as a literal character instead of returning a parse error.
func WithMaxBuffer ¶ added in v1.1.0
WithMaxBuffer caps the Reader's internal buffer at n bytes. A record larger than n cannot be parsed in memory, so Next returns ErrRecordTooLarge rather than letting the buffer grow without bound. A non-positive n means no limit (the default).
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader reads CSV records with zero allocations per record.
The input buffer and the field slice are allocated once in NewReader and reused for the lifetime of the Reader. The fields of a Record returned by Next are zero-copy views into that buffer, so they are valid only until the next call to Next.
func NewReader ¶
NewReader returns a Reader that parses CSV records from r, applying opts. An invalid delimiter marks the Reader as failed; Next and Error report the error.
func (*Reader) Error ¶ added in v0.2.0
Error returns the first error encountered while reading, or nil if none has occurred. io.EOF is normal termination and is not treated as an error, so Error returns nil after a record stream has been read to completion. A Reader configured with an invalid delimiter is failed from the start.
func (*Reader) FieldsPerRecord ¶ added in v1.1.0
FieldsPerRecord returns the expected number of fields per record. It reflects the value configured with WithFieldsPerRecord: with auto-detection (the default) it is 0 until the first record is read, after which it is the field count learned from that record; a negative value means no check is in effect.
func (*Reader) Next ¶
Next parses and returns the next record, or io.EOF when no records remain. It returns ErrBareQuote or ErrQuote for malformed quoting, unless WithLazyQuotes is in effect, and propagates any error from the underlying reader.
If a field count is in effect (see WithFieldsPerRecord) and the record has a different number of fields, Next returns the record along with ErrFieldCount. Like encoding/csv, this error is non-fatal: the returned Record is valid and reading may continue.
The returned Record is reused by the Reader and overwritten by each call to Next. Its fields are zero-copy views into the Reader's internal buffer and are valid only until the next call to Next, like encoding/csv's ReuseRecord mode. Copy a field with Record.Copy, or convert it to a string, if it must outlive the read loop.
type Record ¶
type Record struct {
// contains filtered or unexported fields
}
Record is a parsed CSV record. The Reader reuses a single Record and overwrites it on every call to Next, so a Record is valid only until the Reader is advanced again. Its fields are zero-copy views into the Reader's internal buffer; use Copy to retain the values beyond the next call to Next.
func (*Record) Copy ¶ added in v0.2.0
Copy returns the record's fields as owned strings. The returned slice and its strings remain valid after subsequent calls to Next.
func (*Record) ValueAt ¶
ValueAt returns the field at index idx as a zero-copy view into the Reader's internal buffer. The slice is valid only until the next call to Next on the Reader that produced the Record. Convert it to a string to get an owned copy, since string(v) always copies. It panics if idx is negative or not less than Len.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer writes CSV records with zero allocations per write.
The bufio.Writer and the numeric scratch buffer are allocated once in NewWriter and reused for the lifetime of the Writer. Write/WriteAll perform no heap allocations on the hot path as long as the caller reuses a []Column slice (e.g. Write(row...)) rather than passing freshly constructed variadic args.
func NewWriter ¶ added in v0.2.0
NewWriter returns a Writer that writes CSV records to w, applying opts. If w is already a *bufio.Writer with a buffer at least as large as the default (4096 bytes), it is reused directly rather than being wrapped again.
func (*Writer) Error ¶
Error returns the first error encountered during Write, WriteAll or Flush, or nil if none has occurred.
func (*Writer) FieldsPerRecord ¶ added in v1.1.0
FieldsPerRecord returns the expected number of fields per record, as configured with WithFieldsPerRecord. With auto-detection (the default) it is 0 until the first record is written, after which it is the field count learned from that record; a negative value means no check is in effect.
func (*Writer) Flush ¶
Flush writes any buffered data to the underlying writer and returns the first error encountered during Write, WriteAll or Flush, if any.
func (*Writer) Write ¶
Write writes cols as a single CSV record to the underlying writer. It returns ErrEmptyRecord if cols is empty, or the first error encountered while writing the record.
If a field count is in effect (see WithFieldsPerRecord) and cols has a different number of fields, Write returns ErrFieldCount. Like encoding/csv, the error is non-fatal: the record is still written and writing may continue.