namedcsvreader

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2021 License: MIT Imports: 6 Imported by: 0

README

Named csv reader

Package named csv reader provides methods to easly read csv files and parse columsn to basic types.

This package is not a CSV parser, it uses the encodig/csv reader to parse the csv file.

Installation

go get github.com/dylanvgils/namedcsvreader

Examples

CSV file with headers

Read csv file using the headers in the csv file.

file, err := os.Open("testdata/valid.csv")
if err != nil {
    panic(err)
}

for record := range namedcsvreader.NewReader(file).Read() {
    fmt.Printf("Name: %s; Age: %d\n", record.GetString("name"), record.GetInt("age"))
}

// Output:
// Name: Colorado Leon; Age: 26
// Name: Rajah Fletcher; Age: 47
// Name: Tobias Snow; Age: 16
// Name: Jared Finch; Age: 19
CSV file without headers

Read a csv file that does not contain headers, by specifying your own.

file, err := os.Open("testdata/valid_without_headers.csv")
if err != nil {
    panic(err)
}

reader := namedcsvreader.NewReader(file).
    WithHeaders("name", "age")

for record := range reader.Read() {
    fmt.Printf("Name: %s; Age: %d\n", record.GetString("name"), record.GetInt("age"))
}

// Output:
// Name: Colorado Leon; Age: 26
// Name: Rajah Fletcher; Age: 47

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NamedCsvReader

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

Named csv reader represents the reader instance.

func NewReader

func NewReader(file *os.File) *NamedCsvReader

NewReader returns a new named csv reader for the given file.

func (*NamedCsvReader) Read

func (reader *NamedCsvReader) Read() chan *Record

Read advances the reader to the next record.

func (*NamedCsvReader) WithHeaders

func (reader *NamedCsvReader) WithHeaders(headers ...string) *NamedCsvReader

WithHeaders configures the headers used to read the csv data.

type Record

type Record struct {
	Error  error // Error that occurend when reading the record, if any.
	RowNum int   // The row number of the record

	*NamedCsvReader
	// contains filtered or unexported fields
}

Record represents a single record of the named csv reader.

func (*Record) GetBoolean

func (record *Record) GetBoolean(key string) bool

GetBoolean returns the value of the specified column as boolean. 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False as value. Any other value returns false.

func (*Record) GetFloat32

func (record *Record) GetFloat32(key string) float32

GetFloat32 returns the value of the specified column as float32. When no value is found it returns 0.

func (*Record) GetFloat64

func (record *Record) GetFloat64(key string) float64

GetFloat64 returns the value of the specified column as float64. When no value is found it returns 0.

func (*Record) GetInt

func (record *Record) GetInt(key string) int

GetInt returns the value of the specified column as int. When no value is found it returns 0.

func (*Record) GetInt32

func (record *Record) GetInt32(key string) int32

GetInt32 returns the value of the specified column as int32. When no value is found it returns 0.

func (*Record) GetInt64

func (record *Record) GetInt64(key string) int64

GetInt64 returns the value of the specified column as int64. When no value is found it returns 0.

func (*Record) GetString

func (record *Record) GetString(key string) string

GetString returns the value of the specified column as string. When no value is found it returns an empty string.

func (*Record) GetTime

func (record *Record) GetTime(layout string, key string) time.Time

GetTime returns the value of the specified column as date time. When no value is found it return the default date time. See the time documentation for the available layouts.

Jump to

Keyboard shortcuts

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