Documentation ¶
Overview ¶
Package csvext provides marshaling and unmarshaling for CSV data in Go.
Index ¶
- func Marshal(v interface{}) ([]byte, error)
- func MarshalHeader(v interface{}) ([]string, error)
- func MarshalRecord(v interface{}) ([]string, error)
- func ReadTable(r io.Reader, f func([]string, []string) error) error
- func Unmarshal(data []byte, v interface{}) error
- func UnmarshalRecord(header, record []string, v interface{}) error
- type Decoder
- type Encoder
- type Marshaler
- type Unmarshaler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalHeader ¶
MarshalHeader marshals a struct v into a CSV header.
func MarshalRecord ¶
MarshalRecord marshals a struct v into a CSV record.
func ReadTable ¶
ReadTable reads a CSV table from r, calling f for each record.
Example ¶
package main import ( "fmt" "log" "strings" "github.com/tradyfinance/csvext" ) func main() { type Record struct { Foo string `csv:"foo"` Bar string `csv:"bar"` } r := strings.NewReader("foo,bar\nlorem,ipsum") if err := csvext.ReadTable(r, func(header, record []string) error { var rec Record if err := csvext.UnmarshalRecord(header, record, &rec); err != nil { return err } fmt.Printf("%+v\n", rec) return nil }); err != nil { log.Fatal(err) } }
Output:
func Unmarshal ¶
Unmarshal parses the CSV-encoded data and stores the result in the value pointed to by v.
Example ¶
package main import ( "fmt" "log" "github.com/tradyfinance/csvext" ) func main() { type Record struct { Foo string `csv:"foo"` Bar string `csv:"bar"` } data := []byte("foo,bar\nlorem,ipsum") var records []Record if err := csvext.Unmarshal(data, records); err != nil { log.Fatal(err) } fmt.Printf("%+v\n", records) }
Output:
func UnmarshalRecord ¶
UnmarshalRecord unmarshals a CSV record into a struct v. It will unmarshal fields by name when header is not nil, otherwise it will unmarshal fields by index.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder reads and decodes a CSV table from an input stream.
func NewDecoder ¶
NewDecoder returns a new decoder that reads from r.
func (*Decoder) Decode ¶
Decode reads a CSV table from its input stream and stores it in the value pointed to by v.
Example ¶
package main import ( "fmt" "log" "strings" "github.com/tradyfinance/csvext" ) func main() { type Record struct { Foo string `csv:"foo"` Bar string `csv:"bar"` } r := strings.NewReader("foo,bar\nlorem,ipsum") var records []Record if err := csvext.NewDecoder(r).Decode(&records); err != nil { log.Fatal(err) } fmt.Printf("%+v\n", records) }
Output:
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
A Encoder writes a CSV table to an output stream.
func NewEncoder ¶
NewEncoder returns a new encoder that writes to w.
type Marshaler ¶
Marshaler is the inteface implemented by types that can marshal themselves into valid CSV data.
type Unmarshaler ¶
Unmarshaler is the interface implemented by types that can unmarshal themselves from valid CSV data.