csvlib

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package csvlib provides readers (iterators) and writers for CSV files.

Index

Examples

Constants

View Source
const DefaultBufferSize = 1024 * 4

DefaultBufferSize is the default buffer size used for reading records.

Variables

This section is empty.

Functions

func NewDefaultIterator

func NewDefaultIterator[T any](input io.Reader,
	hasHeader bool,
	conversionFunc ParseFunc[T]) (iter.Seq2[Record[T], error], error)

NewDefaultIterator returns an iterator with a DefaultBufferSize.

Example
// prepare sample data
const csvData = `"first_name","last_name"
"John","Doe"
"Jane","Doe"
`
input := strings.NewReader(csvData)

iter, err := NewDefaultIterator[ExampleRecord](input, true, csvToExampleRecord)
if err != nil {
	fmt.Println("error: ", err.Error())
}

// process each record
for rec, err := range iter {
	if err != nil {
		fmt.Println("error: ", err.Error())
		break
	}
	fmt.Printf("record: %d, first name: %q, last name: %q\n", rec.LineNumber, rec.Data.FirstName, rec.Data.LastName)
}
Output:

record: 2, first name: "John", last name: "Doe"
record: 3, first name: "Jane", last name: "Doe"

func NewIterator

func NewIterator[T any](input io.Reader,
	hasHeader bool,
	bufferSize int,
	conversionFunc ParseFunc[T]) (iter.Seq2[Record[T], error], error)

NewIterator returns a buffered iterator with a configurable buffer size. DefaultBufferSize is used if bufferSize < DefaultBufferSize.

Types

type ConvertFunc

type ConvertFunc[T any] func(T) ([]string, error)

ConvertFunc converts type T to a []string record for CSV writing output.

type IterationError

type IterationError struct {
	// contains filtered or unexported fields
}

IterationError is returned when an error occurs during2 CSV file iteration.

func NewIterationError

func NewIterationError(lineNumber int, cause error) *IterationError

func (*IterationError) Error

func (ie *IterationError) Error() string

Error returns context specific IterationError information.

func (*IterationError) Unwrap

func (ie *IterationError) Unwrap() error

Unwrap returns our inner error, aka "the cause".

type ParseError

type ParseError struct {
	// contains filtered or unexported fields
}

ParseError is returned when an error occurs parsing a raw []string record to type T.

func NewParseError

func NewParseError(lineNumber int, cause error) *ParseError

NewParseError returns a ParseError with the specified context information.

func (*ParseError) Error

func (ce *ParseError) Error() string

Error returns context specific ParseError information.

func (*ParseError) Unwrap

func (ce *ParseError) Unwrap() error

Unwrap returns our inner error, aka "the cause".

type ParseFunc

type ParseFunc[T any] func([]string) (T, error)

ParseFunc is used to parse a CSV record []string into type T

type Record

type Record[T any] struct {
	LineNumber int
	Data       T
}

Record encapsulates a csv record including the record's line number and associated data.

type Writer

type Writer[T any] struct {
	// contains filtered or unexported fields
}

Writer writes csv records to an output file. convertFunc is used to convert type T to []string for CSV writer output.

func NewWriter added in v0.10.0

func NewWriter[T any](output io.Writer, convertFunc ConvertFunc[T]) (Writer[T], error)

NewWriter creates a new Writer which writes records of type T to an output target.

Example
var output bytes.Buffer

writer, err := NewWriter(&output, exampleRecordToCsv)
if err != nil {
	fmt.Println("error creating writer ", err.Error())
	return
}
// writer will flush prior to close
defer writer.Close()

err = writer.WriteHeader([]string{"first_name", "last_name"})
if err != nil {
	fmt.Println("error writing header: ", err.Error())
	return
}

for _, rec := range []ExampleRecord{{"John", "Doe"}, {"Jane", "Doe"}} {
	if err := writer.Write(rec); err != nil {
		fmt.Println("error writing record: ", err.Error())
		break
	}
}
// flush our output so that it will be available to print
writer.Flush()
fmt.Println(output.String())
Output:

first_name,last_name
John,Doe
Jane,Doe

func (*Writer[T]) Close

func (w *Writer[T]) Close() error

Close flushes remaining data to the writer and closes related resources.

func (*Writer[T]) Flush

func (w *Writer[T]) Flush()

Flush writes the current buffer to the output

func (*Writer[T]) Write

func (w *Writer[T]) Write(inputRecord T) error

Write writes the csv record to the output.

func (*Writer[T]) WriteHeader

func (w *Writer[T]) WriteHeader(headerRecord []string) error

WriteHeader writes a header to the output csv file.

Jump to

Keyboard shortcuts

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