csvdec

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2023 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package csvdec provides a generic CSV decoder. Wraps the encoding/csv package with a decoder that can populate structs and slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder struct {
	*csv.Reader
	SkipCols uint // How many columns to skip from the beginning of each line.
}

A Decoder reads CSV lines and converts them to data objects. Embeds a csv.Reader, so it can be used the same way.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r. skipRows and skipCols indicate how many of the first rows and columns should be ignored.

func (*Decoder) Decode

func (d *Decoder) Decode(a interface{}) error

Decode reads the next CSV line and populates the given object with parsed values. Accepted input types are struct pointers and slice pointers, as explained below.

Struct pointer: all fields must be exported and of type int*, uint* float*, string or bool. Fields will be populated by order of appearance. Too few values in the CSV line will result in an error. Excess values in the CSV line will be ignored. The struct's last field may be a slice, in which case all the remaining values will be parsed for that slice's type, according to the restrictions below.

Slice pointer of type int*, uint*, float*, string: the pointer will be populated with a slice of parsed values, according to the length of the CSV line.

Any other type will cause a panic.

func (*Decoder) SkipRow

func (d *Decoder) SkipRow() error

SkipRow skips a row and returns an error if reading failed.

Jump to

Keyboard shortcuts

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