Documentation
¶
Index ¶
- Variables
- type 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 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
- func ColumnValuer(v FieldValuer) Column
- type ColumnKind
- type FieldScanner
- type FieldValuer
- 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 Read 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 Read 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 all constructors.
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 and no heap allocation.
func ColumnFloat32 ¶
ColumnFloat32 returns a Column containing v.
func ColumnFloat64 ¶
ColumnFloat64 returns a Column containing v.
func ColumnUintptr ¶
ColumnUintptr returns a Column containing v.
func ColumnValuer ¶ added in v1.2.0
func ColumnValuer(v FieldValuer) Column
ColumnValuer returns a Column containing v, which appends its CSV representation with zero heap allocations.
func (Column) Kind ¶ added in v1.2.0
func (c Column) Kind() ColumnKind
Kind returns the ColumnKind of c.
type ColumnKind ¶
type ColumnKind uint8
ColumnKind identifies the payload type stored in a Column.
const ( ColumnKindString ColumnKind = iota ColumnKindBytes ColumnKindInt ColumnKindUint ColumnKindFloat ColumnKindFloat32 ColumnKindBool ColumnKindValuer )
type FieldScanner ¶ added in v1.2.0
FieldScanner is implemented by custom types that can scan their value directly from a raw CSV field byte slice.
type FieldValuer ¶ added in v1.2.0
FieldValuer is implemented by custom types that can append their CSV field representation directly into a scratch buffer with zero heap allocations.
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. Only single ASCII bytes are supported. The NUL byte, '"', '\r', '\n' and any byte above '\x7f' are rejected: an invalid delimiter marks a Writer or Reader as failed, and Read, ReadAll, 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, Read, ReadAll 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 Read 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. Read returns records lazily one at a time with zero heap allocations, while ReadAll eagerly reads all remaining records into a slice of owned Records.
func NewReader ¶
NewReader returns a Reader that parses CSV records from r, applying opts. An invalid delimiter marks the Reader as failed; Read, ReadAll 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) Read ¶ added in v1.2.0
Read reads one record from r. The returned Record provides zero-allocation access to fields via Scan, or safe access via String, Bytes, and Strings.
If the record has an unexpected number of fields (see WithFieldsPerRecord), Read returns the Record along with ErrFieldCount. Like encoding/csv, this error is non-fatal: the record is usable and subsequent calls to Read continue reading the stream.
If no more records remain, Read returns a zero Record with io.EOF.
func (*Reader) ReadAll ¶ added in v1.2.0
ReadAll reads all remaining records from r into a slice of Records. Each Record in the returned slice owns its field data and remains valid indefinitely. A successful call returns err == nil (io.EOF is treated as normal completion).
If an error (such as ErrFieldCount or a parse error) is encountered, ReadAll stops immediately and returns the records read so far along with the error, matching encoding/csv.ReadAll semantics.
type Record ¶
type Record struct {
// contains filtered or unexported fields
}
Record is a single CSV record parsed by Reader.
A Record obtained from Read() provides safe, encapsulated access to the parsed fields. To achieve zero heap allocations on the hot path, field data is accessed via Scan (for typed values or reusable []byte buffers), String, Bytes, or Strings.
func (Record) Bytes ¶ added in v1.2.0
Bytes copies the field at index idx into dst and returns the resulting slice. If cap(dst) is large enough, Bytes performs zero heap allocations. It panics if idx is out of range [0, Len()).
func (Record) Error ¶ added in v1.2.0
Error returns the non-fatal error associated with this record (e.g. ErrFieldCount), or nil if the record had no errors.
func (Record) IsFirst ¶ added in v1.2.0
IsFirst reports whether this record is the first non-blank record read from the stream, useful for header detection.
func (Record) Scan ¶ added in v1.2.0
Scan parses the record's fields into the destination pointers, one per field, in order.
Supported destination types:
- *string: copies field as a string
- *[]byte: copies field into caller's slice capacity (0 allocs if capacity suffices)
- *bool: parses boolean ("true", "false", "1", "0", ...) in-place (0 allocs)
- *int, *int8, *int16, *int32, *int64: parses integer in-place (0 allocs)
- *uint, *uint8, *uint16, *uint32, *uint64, *uintptr: parses unsigned integer in-place (0 allocs)
- *float32, *float64: parses float in-place (0 allocs)
- FieldScanner: delegates parsing to custom ScanCSV method (0 allocs)
Scan returns an error if the number of destinations does not match Len(), if any destination pointer is nil, or if parsing fails.
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.