gocsv

package module
v0.0.0-...-93c7852 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2017 License: MIT Imports: 11 Imported by: 0

README

Go CSV

The GoCSV package aims to provide easy serialization and deserialization functions to use CSV in Golang

API and techniques inspired from https://godoc.org/gopkg.in/mgo.v2

GoDoc Build Status

Installation

go get -u github.com/gocarina/gocsv

Full example

Consider the following CSV file


client_id,client_name,client_age
1,Jose,42
2,Daniel,26
3,Vincent,32

Easy binding in Go!


package main

import (
	"fmt"
	"gocsv"
	"os"
)

type Client struct { // Our example struct, you can use "-" to ignore a field
	Id      string `csv:"id"`
	Name    string `csv:"name"`
	Age     string `csv:"age"`
	NotUsed string `csv:"-"`
}

func main() {
	clientsFile, err := os.OpenFile("clients.csv", os.O_RDWR|os.O_CREATE, os.ModePerm)
	if err != nil {
		panic(err)
	}
	defer clientsFile.Close()

	clients := []*Client{}

	if err := gocsv.UnmarshalFile(clientsFile, &clients); err != nil { // Load clients from file
		panic(err)
	}
	for _, client := range clients {
		fmt.Println("Hello", client.Name)
	}

	if _, err := clientsFile.Seek(0, 0); err != nil { // Go to the start of the file
		panic(err)
	}

	clients = append(clients, &Client{Id: "12", Name: "John", Age: "21"}) // Add clients
	clients = append(clients, &Client{Id: "13", Name: "Fred"})
	clients = append(clients, &Client{Id: "14", Name: "James", Age: "32"})
	clients = append(clients, &Client{Id: "15", Name: "Danny"})
	csvContent, err := gocsv.MarshalString(&clients) // Get all clients as CSV string
	//err = gocsv.MarshalFile(&clients, clientsFile) // Use this to save the CSV back to the file
	if err != nil {
		panic(err)
	}
	fmt.Println(csvContent) // Display all clients as CSV string

}

Customizable Converters


type DateTime struct {
	time.Time
}

// Convert the internal date as CSV string
func (date *DateTime) MarshalCSV() (string, error) {
	return date.Time.Format("20060201"), nil
}

// You could also use the standard Stringer interface 
func (date *DateTime) String() (string) {
	return date.String() // Redundant, just for example
}

// Convert the CSV string as internal date
func (date *DateTime) UnmarshalCSV(csv string) (err error) {
	date.Time, err = time.Parse("20060201", csv)
	if err != nil {
		return err
	}
	return nil
}

type Client struct { // Our example struct with a custom type (DateTime)
	Id       string   `csv:"id"`
	Name     string   `csv:"name"`
	Employed DateTime `csv:"employed"`
}

Customizable CSV Reader / Writer


func main() {
	...
	
	gocsv.SetCSVReader(func(in io.Reader) *csv.Reader {
    	//return csv.NewReader(in)
    	return gocsv.LazyCSVReader(in) // Allows use of quotes in CSV
    })

    ...

    gocsv.UnmarshalFile(file, &clients)

    ...

    gocsv.SetCSVWriter(func(out io.Writer) *csv.Writer {
    	return csv.NewWriter(out)
    })

    ...

    gocsv.MarshalFile(&clients, file)

	...
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyCSV = errors.New("empty csv file given")
)
View Source
var FailIfDoubleHeaderNames = false

FailIfDoubleHeaderNames indicates whether it is considered an error when a header name is repeated in the csv header.

View Source
var FailIfUnmatchedStructTags = true

FailIfUnmatchedStructTags indicates whether it is considered an error when there is an unmatched struct tag.

View Source
var ShouldAlignDuplicateHeadersWithStructFieldOrder = false

ShouldAlignDuplicateHeadersWithStructFieldOrder indicates whether we should align duplicate CSV headers per their alignment in the struct definition.

View Source
var TagSeparator = ","

TagSeparator defines seperator string for multiple csv tags in struct fields

Functions

func CSVToMap

func CSVToMap(in io.Reader) (map[string]string, error)

func DefaultCSVReader

func DefaultCSVReader(in io.Reader) *csv.Reader

DefaultCSVReader is the default CSV reader used to parse CSV (cf. csv.NewReader)

func DefaultCSVWriter

func DefaultCSVWriter(out io.Writer) *csv.Writer

DefaultCSVWriter is the default CSV writer used to format CSV (cf. csv.NewWriter)

func LazyCSVReader

func LazyCSVReader(in io.Reader) *csv.Reader

LazyCSVReader returns a lazy CSV reader, with LazyQuotes and TrimLeadingSpace.

func Marshal

func Marshal(in interface{}, out io.Writer) (err error)

Marshal returns the CSV in writer from the interface.

func MarshalBytes

func MarshalBytes(in interface{}) (out []byte, err error)

MarshalBytes returns the CSV bytes from the interface.

func MarshalCSV

func MarshalCSV(in interface{}, out *csv.Writer) (err error)

MarshalCSV returns the CSV in writer from the interface.

func MarshalCSVWithoutHeaders

func MarshalCSVWithoutHeaders(in interface{}, out *csv.Writer) (err error)

MarshalCSV returns the CSV in writer from the interface.

func MarshalChan

func MarshalChan(c <-chan interface{}, out *csv.Writer) error

MarshalChan returns the CSV read from the channel.

func MarshalFile

func MarshalFile(in interface{}, file *os.File) (err error)

MarshalFile saves the interface as CSV in the file.

func MarshalString

func MarshalString(in interface{}) (out string, err error)

MarshalString returns the CSV string from the interface.

func MarshalWithoutHeaders

func MarshalWithoutHeaders(in interface{}, out io.Writer) (err error)

Marshal returns the CSV in writer from the interface.

func SetCSVReader

func SetCSVReader(csvReader func(io.Reader) *csv.Reader)

SetCSVReader sets the CSV reader used to parse CSV.

func SetCSVWriter

func SetCSVWriter(csvWriter func(io.Writer) *csv.Writer)

SetCSVWriter sets the CSV writer used to format CSV.

func Unmarshal

func Unmarshal(in io.Reader, out interface{}) (err error)

Unmarshal parses the CSV from the reader in the interface.

func UnmarshalBytes

func UnmarshalBytes(in []byte, out interface{}) (err error)

UnmarshalBytes parses the CSV from the bytes in the interface.

func UnmarshalBytesToCallback

func UnmarshalBytesToCallback(in []byte, f interface{}) (err error)

UnmarshalBytesToCallback parses the CSV from the bytes and send each value to the given func f. The func must look like func(Struct).

func UnmarshalBytesToChan

func UnmarshalBytesToChan(in []byte, c interface{}) error

UnmarshalBytesToChan parses the CSV from the bytes and send each value in the chan c. The channel must have a concrete type.

func UnmarshalCSV

func UnmarshalCSV(in CSVReader, out interface{}) error

UnmarshalCSV parses the CSV from the reader in the interface.

func UnmarshalFile

func UnmarshalFile(in *os.File, out interface{}) (err error)

UnmarshalFile parses the CSV from the file in the interface.

func UnmarshalString

func UnmarshalString(in string, out interface{}) (err error)

UnmarshalString parses the CSV from the string in the interface.

func UnmarshalStringToCallback

func UnmarshalStringToCallback(in string, c interface{}) (err error)

UnmarshalStringToCallback parses the CSV from the string and send each value to the given func f. The func must look like func(Struct).

func UnmarshalStringToChan

func UnmarshalStringToChan(in string, c interface{}) error

UnmarshalStringToChan parses the CSV from the string and send each value in the chan c. The channel must have a concrete type.

func UnmarshalToCallback

func UnmarshalToCallback(in io.Reader, f interface{}) (err error)

UnmarshalToCallback parses the CSV from the reader and send each value to the given func f. The func must look like func(Struct).

func UnmarshalToChan

func UnmarshalToChan(in io.Reader, c interface{}) error

UnmarshalToChan parses the CSV from the reader and send each value in the chan c. The channel must have a concrete type.

Types

type CSVReader

type CSVReader interface {
	Read() ([]string, error)
	ReadAll() ([][]string, error)
}

type Decoder

type Decoder interface {
	// contains filtered or unexported methods
}

Decoder .

type MissingColumnsError

type MissingColumnsError struct {
	MissingColumnNames []string
}

func (MissingColumnsError) Error

func (e MissingColumnsError) Error() string

type NoMarshalFuncError

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

NoMarshalFuncError is the custom error type to be raised in case there is no marshal function defined on type

func (NoMarshalFuncError) Error

func (e NoMarshalFuncError) Error() string

type NoUnmarshalFuncError

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

NoUnmarshalFuncError is the custom error type to be raised in case there is no unmarshal function defined on type

func (NoUnmarshalFuncError) Error

func (e NoUnmarshalFuncError) Error() string

type SimpleDecoder

type SimpleDecoder interface {
	// contains filtered or unexported methods
}

SimpleDecoder .

type Stringer

type Stringer interface {
	String() string
}

Stringer is implemented by any value that has a String method This converter is used to convert the value to it string representation This converter will be used if your value does not implement TypeMarshaller

type TypeMarshaller

type TypeMarshaller interface {
	MarshalCSV() (string, error)
}

TypeMarshaller is implemented by any value that has a MarshalCSV method This converter is used to convert the value to it string representation

type TypeUnmarshaller

type TypeUnmarshaller interface {
	UnmarshalCSV(string) error
}

TypeUnmarshaller is implemented by any value that has an UnmarshalCSV method This converter is used to convert a string to your value representation of that string

Jump to

Keyboard shortcuts

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