csvcolumn

package module
v0.0.0-...-4bdc5d8 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2020 License: MIT Imports: 4 Imported by: 0

README

csvcolumn

GoDoc

csvcolumn package implements convenient CSV reading with column access.

Internally it uses https://golang.org/pkg/encoding/csv/, which means it inherits all the same restrictions.

const CSV = `Index,Age,Name
1,52,Alice
5,42,Bob
512,31,Charlie
`

func Example() {
	source := strings.NewReader(CSV)

	data := csvcolumn.NewReader(source)
	data.LazyQuotes = true
	name, age := data.String("Name"), data.Int("Age")

	for data.Next() && data.Err() == nil {
		fmt.Println(*name, *age)
	}

	if data.Err() != nil {
		fmt.Println(data.Err())
	}
}

Documentation

Overview

Package csvcolumn reads specified columns from a csv style input.

Index

Examples

Constants

View Source
const (
	// ColumnUnbound is an init value of a field.Column
	ColumnUnbound = -2
	// ColumnMissing is an field.Column value used when field.Name was not
	// found in the headers of the source
	ColumnMissing = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Float64

type Float64 struct {
	Value   float64
	Default float64
	Error   error
}

Float64 is a struct implementing the Value interface

func (*Float64) Scan

func (value *Float64) Scan(text string) error

func (*Float64) String

func (value *Float64) String() string

type Int

type Int struct {
	Value   int
	Default int
	Error   error
}

Int is a struct implementing the Value interface

func (*Int) Scan

func (value *Int) Scan(text string) error

func (*Int) String

func (value *Int) String() string

type Reader

type Reader struct {
	Comma               rune
	Comment             rune
	FieldsPerRecord     int
	LazyQuotes          bool
	TrimLeadingSpace    bool
	CaseSensitiveHeader bool
	// contains filtered or unexported fields
}

Reader keeps the configuration and state of reading from source

Example
package main

import (
	"fmt"
	"strings"

	"github.com/loov/csvcolumn"
)

func main() {
	source := strings.NewReader(`Index,Age,Height,Name
1,52,1.7,Alice
5,42,1.88,Bob
512,31,1.82,Charlie`)

	data := csvcolumn.NewReader(source)
	data.LazyQuotes = true
	name, age, height := data.String("Name"), data.Int("Age"), data.Float64("Height")

	for data.Next() && data.Err() == nil {
		fmt.Println(*name, *age, *height)
	}

	if data.Err() != nil {
		fmt.Println(data.Err())
	}

}
Output:

Alice 52 1.7
Bob 42 1.88
Charlie 31 1.82

func NewReader

func NewReader(src io.Reader) *Reader

NewReader is a factory function to create a *Reader

func (*Reader) Bind

func (r *Reader) Bind(columnName string, value Value)

Bind binds a column in a csv to its matching Value struct. It's useful when you are interested in adding your own Value types.

func (*Reader) Err

func (r *Reader) Err() error

Err returns the latest error of a reader.

func (*Reader) Float64

func (r *Reader) Float64(columnName string) *float64

Float64 returns a pointer to an float64 field value of a given columnName which is reassigned with every Next() call.

func (*Reader) Int

func (r *Reader) Int(columnName string) *int

Int returns a pointer to an int field value of a given columnName which is reassigned with every Next() call.

func (*Reader) Next

func (r *Reader) Next() (ok bool)

Next parses a next line from a source.

func (*Reader) String

func (r *Reader) String(columnName string) *string

String returns a pointer to a string field value of a given columnName which is reassigned with every Next() call.

type String

type String struct {
	Value string
}

String is a struct implementing the Value interface

func (*String) Scan

func (value *String) Scan(text string) error

type Value

type Value interface {
	// Scan assigns a value from text
	//
	// An error should be returned if the value cannot be stored
	// without loss of information.
	Scan(text string) error
}

Value interface describes a way how different data types should be scanned.

Jump to

Keyboard shortcuts

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