encoding: github.com/atotto/encoding/csv Index | Examples | Files

package csv

import "github.com/atotto/encoding/csv"

Package csv reads and writes comma-separated values (CSV) files.

You can use flat struct data type to recieve the csv data.

This package are an extended version of encoding/csv package. See encoding/csv package : http://golang.org/pkg/encoding/csv/

Index

Examples

Package Files

doc.go read_struct.go write_struct.go

type Reader Uses

type Reader struct {
    *csv.Reader
}

A Reader reads records from a CSV-encoded file.

Code:

csvtext := "Gopher,2,12.3,34.5\nGhost,4,12.3,3.4e-05"

type Person struct {
    Name   string
    Age    int
    Height float32
    Weight float64
}

var person = []Person{}

w := NewReader(strings.NewReader(csvtext))
err := w.ReadStructAll(&person)
if err != nil {
    return
}

fmt.Printf("%v", person)

Output:

[{Gopher 2 12.3 34.5} {Ghost 4 12.3 3.4e-05}]

func NewReader Uses

func NewReader(r io.Reader) *Reader

NewReader returns a new Reader that reads from r.

func (*Reader) ReadStruct Uses

func (r *Reader) ReadStruct(v interface{}) (err error)

func (*Reader) ReadStructAll Uses

func (r *Reader) ReadStructAll(v interface{}) (err error)

type UnsupportedTypeError Uses

type UnsupportedTypeError struct {
    Type reflect.Type
}

An UnsupportedTypeError is returned by Writer when attempting to encode an unsupported value type.

func (*UnsupportedTypeError) Error Uses

func (e *UnsupportedTypeError) Error() string

type Writer Uses

type Writer struct {
    *csv.Writer
}

A Writer writes records to a CSV encoded file.

see encoding/csv package.

Code:

type Person struct {
    Name   string
    Age    int
    Height float32
    Weight float64
}

member := []Person{
    {"Gopher", 2, 12.3, 34.5},
    {"Ghost", 4, 12.3, 0.000034},
}

w := NewWriter(os.Stdout)
w.WriteStructAll(member)

Output:

Gopher,2,12.3,34.5
Ghost,4,12.3,3.4e-05

func NewWriter Uses

func NewWriter(w io.Writer) *Writer

NewWriter returns a new Writer that writes to w.

func (*Writer) WriteStruct Uses

func (w *Writer) WriteStruct(v interface{}) (err error)

WriteStruct writes a single CSV record to w along with any necessary quoting. A record is a struct of flat structure.

func (*Writer) WriteStructAll Uses

func (w *Writer) WriteStructAll(v interface{}) (err error)

WriteStructAll write

func (*Writer) WriteStructHeader Uses

func (w *Writer) WriteStructHeader(v interface{}) (err error)

Package csv imports 5 packages (graph) and is imported by 4 packages. Updated 2018-12-20. Refresh now. Tools for package owners.