csv

package
v0.1.74 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2025 License: MIT Imports: 12 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 D

type D []E

D is an ordered representation of a document. This type should be used when the order of the elements matter.

type E

type E struct {
	Key   string
	Value any
}

E represents a element for a D. It is usually used inside a D.

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

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

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)

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) SkipWriteFields

func (w *Writer) SkipWriteFields()

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