csv

package module
v0.0.0-...-d251d7b Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

csv

Library to work with CSV files

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

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

Reader is a structured data reader from CSV.

Example
r, err := NewReader[*exampleType](csv.NewReader(strings.NewReader(exampleCSV)))
if err != nil {
	log.Fatal(err)
}
for {
	var record exampleType
	if err := r.Read(&record); err != nil {
		if err == io.EOF {
			break
		}
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", record)
}
Output:

{Bar:2 Baz:hello Foo:1}
{Bar:2 Baz:world Foo:3}

func NewReader

func NewReader[T any](r *csv.Reader) (*Reader[T], error)

NewReader creates a new structured data reader from an underlying raw CSV record reader. It returns error if the generic type T is not a valid type to stored the parsed data.

func (*Reader[T]) Read

func (r *Reader[T]) Read(rowPtr T) error

Read reads one record as rowPtr. It returns io.EOF if there's no more record to read.

type Tag

type Tag struct {
	// FieldHeader is the CSV header value of the field.
	FieldHeader string
	Options     string
}

Tag represents a "csv" struct field tag.

For example, `csv:"field_name"` is represented as Tag{FieldName: "field_name"}

func ParseTag

func ParseTag(tag string) Tag

ParseTag parses a raw struct tag (`csv:"tag,value,value2"`) and returns a Tag representing its content.

Directories

Path Synopsis
cmd
gen
Command gen generates code to parse a CSV record to a struct.
Command gen generates code to parse a CSV record to a struct.

Jump to

Keyboard shortcuts

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