Documentation ¶
Index ¶
- func ColumnNames[T any]() []string
- func FieldsValue(v any) ([]string, error)
- func Marshal[T any](w *csv.Writer, values iter.Seq[T]) error
- func MarshalFile[T any](filepath string, values iter.Seq[T]) error
- func Unmarshal[T any](r *csv.Reader, opts ...UnmarshalOpt) iter.Seq2[T, error]
- func UnmarshalFile[T any](filepath string, opts ...UnmarshalOpt) (iter.Seq2[T, error], error)
- type RecordUnmarshalingError
- type UnmarshalOpt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ColumnNames ¶
ColumnNames returns the names of the fields in a struct. It can be used with Marshal to write a CSV file with a column header row. If a field has a `csv` tag, its value will be used instead of the field name. Note that this does not support `csv:"-"`.
func FieldsValue ¶
FieldsValue returns the values of the fields in a struct as a slice of strings (a CSV row).
func MarshalFile ¶
MarshalFile writes the CSV representation of values to filepath.
func UnmarshalFile ¶
UnmarshalFile reads the CSV file from filepath and unmarshals it into v.
Types ¶
type RecordUnmarshalingError ¶
type RecordUnmarshalingError struct { Record []string `json:"record"` Line int `json:"line"` // contains filtered or unexported fields }
RecordUnmarshalingError is an error that occurs when unmarshaling a single record. It contains thee record itself and the error that occurred.
func (*RecordUnmarshalingError) Error ¶
func (e *RecordUnmarshalingError) Error() string
Error returns the error message.
func (*RecordUnmarshalingError) Unwrap ¶
func (e *RecordUnmarshalingError) Unwrap() error
Unwrap returns the underlying error.
type UnmarshalOpt ¶
type UnmarshalOpt func(o *unmarshalOpts)
UnmarshalOpt is a function that modifies the behavior of Unmarshal.
func AllowMissingFields ¶
func AllowMissingFields() UnmarshalOpt
AllowMissingFields allows the CSV file to potentially have fewer columns than the struct. Columns that are missing will be set to their zero value.
Note that Unmarshal will always allow the CSV file to have *more* columns than the struct regardless if this option is used.
func ErrorEarly ¶
func ErrorEarly() UnmarshalOpt
ErrorEarly stops unmarshalling as soon as an error is encountered.
func SkipHeader ¶
func SkipHeader() UnmarshalOpt
SkipHeader skips the first row of the CSV file if it matches the column names of the struct.