csvx

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package csvx provides convenience wrappers around encoding/csv.

It provides a set of Decode functions, that populate struct fields with delimiter-separated values.

Decode Accepted Types

The T type parameter for Decode functions accepts structs. Fields may be of types bool, int*, uint*, float* or string for automatic parsing. For manual parsing with a method, any type is allowed. Unexported fields are ignored.

Decode Default Behavior

DecodeFile and DecodeReader match column to field according to their order. For example:

type a struct {
  Name   string  // Matches first column
  Age    int     // Matches second column
  Height float64 // Matches third column
}

DecodeFileHeader and DecodeReaderHeader match column to field according to the first line and the field's name, case insensitively. For example:

type a struct {
  Name   string  // Matches a column titled name, Name, nAmE, etc.
  Age    int     // Matches a column titled age, Age, aGe, etc.
  Height float64 // Matches a column titled height, Height, hEiGhT, etc.
}

Note that the Header functions use the first line as metadata, while the other two functions expect data starting from the first line. In all functions yielding continues upon parsing errors, so that a caller may choose to skip lines.

Decode Field Tags

Field tags can be used to change the default behavior. The format for a field tag is as follows:

Field int `csvx:"column,modifier1,modifier2..."`

The column part may be:

  • empty: use the default behavior
  • column name or index: associate this field with the column with this name or at this 0-based index, case sensitively
  • "-": a single hyphen, ignore this field entirely

Modifiers may be:

  • "allowempty": the input value may be empty, in which case no parsing will be attempted
  • "optional": don't err if the column for this field is missing
  • exported method name: use T's method with this name to parse the input value

A custom parsing method will replace the default parsing. The method's signature must take a string as input, and return the field's type and an error.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeFile

func DecodeFile[T any](file string, mods ...ReaderModifier) iter.Seq2[T, error]

DecodeFile returns an iterator over parsed instances of T, using column numbers for matching columns to fields.

fn is an optional function for modifying the CSV parser, for example for changing the delimiter.

func DecodeFileHeader

func DecodeFileHeader[T any](file string, mods ...ReaderModifier) iter.Seq2[T, error]

DecodeFileHeader returns an iterator over parsed instances of T, using the first line for matching columns to fields.

fn is an optional function for modifying the CSV parser, for example for changing the delimiter.

func DecodeReader

func DecodeReader[T any](r io.Reader, mods ...ReaderModifier) iter.Seq2[T, error]

DecodeReader returns an iterator over parsed instances of T, using column numbers for matching columns to fields.

fn is an optional function for modifying the CSV parser, for example for changing the delimiter.

Example
type person struct {
	Name string
	Age  int
}
input := strings.NewReader("alice,30\nbob,25")

for p, err := range DecodeReader[person](input) {
	if err != nil {
		panic(err)
	}
	fmt.Println(p.Name, "is", p.Age, "years old")
}
Output:

alice is 30 years old
bob is 25 years old

func DecodeReaderHeader

func DecodeReaderHeader[T any](r io.Reader, mods ...ReaderModifier) iter.Seq2[T, error]

DecodeReaderHeader returns an iterator over parsed instances of T, using the first line for matching columns to fields.

fn is an optional function for modifying the CSV parser, for example for changing the delimiter.

Example
type person struct {
	Name string
	Age  int
}
input := strings.NewReader(
	"user_id,age,city,name\n" +
		"111,30,paris,alice\n" +
		"222,25,london,bob")

for p, err := range DecodeReaderHeader[person](input) {
	if err != nil {
		panic(err)
	}
	fmt.Println(p.Name, "is", p.Age, "years old")
}
Output:

alice is 30 years old
bob is 25 years old

func File

func File(file string, mods ...ReaderModifier) iter.Seq2[[]string, error]

File iterates over CSV entries from a file. Applies the given modifiers before iteration.

func Reader

func Reader(r io.Reader, mods ...ReaderModifier) iter.Seq2[[]string, error]

Reader iterates over CSV entries from a reader. Applies the given modifiers before iteration.

func TSV

func TSV(r *csv.Reader)

TSV makes the reader use tab as the delimiter.

Types

type ReaderModifier

type ReaderModifier = func(*csv.Reader)

ReaderModifier modifies the settings of a CSV reader before iteration starts.

Jump to

Keyboard shortcuts

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