csv

package module
v0.0.0-...-18efe6f Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2017 License: MIT Imports: 7 Imported by: 6

README

csv2

Parse csv files directly into struct instances.

Build Status

Quickstart

Parse the csv file:

ID,NAME,ABBREV
1,"United States",US
2,"Canada",CA
package main

import (
    "fmt"
    "os"

    "github.com/aodin/csv2" // Will import as csv!
)

type Country struct {
    ID     int64
    Name   string
    Abbrev string
}

func (c Country) String() string {
    return fmt.Sprintf("%d: %s", c.ID, c.Name)
}

func main() {
    f, err := os.Open("./countries.csv")
    if err != nil {
        panic(err)
    }
    csvf := csv.NewReader(f)

    // Discard the header
    if _, err = csvf.Read(); err != nil {
        panic(err)
    }

    var countries []Country
    if err = csvf.Unmarshal(&countries); err != nil {
        panic(err)
    }
    fmt.Println(countries)
}

Destination Types

Supported field types for destination structs include:

  • string
  • int64
  • float64
  • bool
  • time.Time

By default, time.Time fields will be parsed with the RFC3339 layout. Alternative layouts can be specified by adding a csv tag to the field:

type holiday struct {
    Name string
    Day  time.Time `csv:"Jan _2"`
}

When a csv column is empty, the types above will error. You will want to use a pointer type, which will be set to nil when there is no content:

  • *int64
  • *float64
  • *bool
  • *time.Time

Writer

The package can also write slices of structs to an output, including a header row derived from struct field names!

f, err := os.OpenFile("out.csv", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
    panic(err)
}
defer f.Close()

writer := csv.NewWriter(f)
writer.WriteHeader(&countries)
writer.Marshal(&countries)
ID,Name,Abbrev
1,United States,US
2,Canada,CA

Happy Hacking!

aodin, 2014

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotPointer = errors.New("csv2: non-pointer receiver")
	ErrNotSlice   = errors.New("csv2: receiver must be a slice of structs")
	ErrNotStruct  = errors.New("csv2: receiver must be a struct")
	ErrUnwritable = errors.New("csv2: writers accept struct or struct slices")
)

Functions

func GetFieldNames

func GetFieldNames(i interface{}) ([]string, error)

GetFieldNames returns a string array of the given interface's field names if the given interface is a struct or slice of structs

Types

type Reader

type Reader struct {
	*csv.Reader
	// contains filtered or unexported fields
}

Reader wraps the csv.Reader and adds a map of csv struct tags.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a new csv2 Reader by wrapping a csv Reader.

func (*Reader) Unmarshal

func (r *Reader) Unmarshal(i interface{}) error

Unmarshal reads the entire Reader into the given destination. The destination interface must of pointer of type slice.

func (*Reader) UnmarshalOne

func (r *Reader) UnmarshalOne(i interface{}) error

UnmarshalOne reads a single row of the Reader into the given struct. The destination interface must of pointer of type struct.

type Writer

type Writer struct {
	*csv.Writer
	// contains filtered or unexported fields
}

Writer wraps the csv.Writer and adds a map of csv struct tags.

func NewWriter

func NewWriter(r io.Writer) *Writer

NewWriter returns a new csv2 Writer by wrapping a csv Writer.

func (*Writer) Marshal

func (w *Writer) Marshal(i interface{}) error

Marshal writes a slice of structs to the Writer. The destination interface must of pointer of type slice.

func (*Writer) WriteHeader

func (w *Writer) WriteHeader(i interface{}) error

WriteHeader will write the names of the underlying struct fields as a row. It accepts a struct or a slice of structs.

Jump to

Keyboard shortcuts

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