csv

package
v0.1.83 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeAll

func DecodeAll[S ~[]E, E any](r io.Reader, dest *S) (err error)

DecodeAll decodes each record from r into dest.

func DecodeFile

func DecodeFile[S ~[]E, E any](file string, dest *S) error

DecodeFile decodes each record from file into dest.

func Export

func Export[S ~[]E, E any](fieldnames []string, slice S, w io.Writer) error

Export writes slice as csv format with fieldnames to writer w.

func ExportFile

func ExportFile[S ~[]E, E any](fieldnames []string, slice S, file string) error

ExportFile writes slice as csv format with fieldnames to file.

func ExportUTF8

func ExportUTF8[S ~[]E, E any](fieldnames []string, slice S, w io.Writer) error

ExportUTF8 writes slice as utf8 csv format with fieldnames to writer w.

func ExportUTF8File

func ExportUTF8File[S ~[]E, E any](fieldnames []string, slice S, file string) error

ExportUTF8File writes slice as utf8 csv format with fieldnames to file.

Types

type Reader

type Reader struct {
	*csv.Reader
	// contains filtered or unexported fields
}

A Reader reads records from a CSV-encoded file.

func NewReader

func NewReader(r io.Reader, hasFields bool) (*Reader, error)

NewReader returns a new Reader that reads from r.

func ReadFile

func ReadFile(file string, hasFields bool) (*Reader, error)

ReadFile returns Reader reads from file.

func (*Reader) Close

func (r *Reader) Close() error

Close closes the underlying reader if it implements the io.Closer interface.

func (*Reader) Decode

func (r *Reader) Decode(dest any) error

Decode will unmarshal the current record into dest. If column's value is like "[...]", it will be treated as slice.

func (*Reader) Next

func (r *Reader) Next() bool

Next prepares the next record for reading with the Scan or Decode method.

func (*Reader) Read

func (r *Reader) Read() (record []string, err error)

Read reads one record (a slice of fields) from r. If the record has an unexpected number of fields, Read returns the record along with the error [ErrFieldCount]. If the record contains a field that cannot be parsed, Read returns a partial record along with the parse error. The partial record contains all fields read before the error. If there is no data left to be read, Read returns nil, io.EOF. If [Reader.ReuseRecord] is true, the returned slice may be shared between multiple calls to Read.

func (*Reader) Scan

func (r *Reader) Scan(dest ...any) error

Scan copies the columns in the current record into the values pointed at by dest. The number of values in dest must be the same as the number of columns in record.

func (*Reader) SetFields

func (r *Reader) SetFields(fields []string)

SetFields sets csv fields.

type Writer

type Writer struct {
	*csv.Writer
	// contains filtered or unexported fields
}

A Writer writes records using CSV encoding.

func NewWriter

func NewWriter(w io.Writer, utf8bom bool) *Writer

NewWriter returns a new Writer that writes to w.

func (*Writer) Write

func (w *Writer) Write(record any) error

Write writes a single CSV record to w along with any necessary quoting after fieldnames is written. A record is a map of strings or a struct. Writes are buffered, so Flush must eventually be called to ensure that the record is written to the underlying io.Writer.

func (*Writer) WriteAll

func (w *Writer) WriteAll(records any) error

WriteAll writes multiple CSV records to w using Write and then calls Flush, returning any error from the Flush.

func (*Writer) WriteFields

func (w *Writer) WriteFields(fields any) error

WriteFields writes fieldnames to w along with necessary utf8bom bytes. The fields must be a non-zero field struct or a non-zero length string slice, otherwise an error will be return. It can be run only once.

Jump to

Keyboard shortcuts

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