googlefinance: github.com/tiloso/googlefinance/csvdecoding Index | Examples | Files

package csvdecoding

import "github.com/tiloso/googlefinance/csvdecoding"

Package csvdecoding implements decoding of CSV streams (from googlefinance) into slices of Go structs.

It is build on top of Go's standard library package encoding/csv and takes some inspiration from Unmarshal of encoding/json.

The package has only been tested with CSV streams from googlefinance so far and it only allows to decode CSV data into a slice of structs.

Code:

r := strings.NewReader(`
Date,Open,High,Low,Close,Volume
21-Oct-14,24.22,24.95,23.98,24.90,44253
20-Oct-14,24.38,24.38,23.95,24.19,58890
`)

type Quote struct {
    Date                   time.Time
    Open, High, Low, Close float64
    Volume                 uint32
}

var quotes []Quote
if err := csvdecoding.New(r).Decode(&quotes); err != nil {
    fmt.Printf("err: %v\n", err)
}
fmt.Printf("%+v\n", quotes)

Output:

[{Date:2014-10-21 00:00:00 +0000 UTC Open:24.22 High:24.95 Low:23.98 Close:24.9 Volume:44253} {Date:2014-10-20 00:00:00 +0000 UTC Open:24.38 High:24.38 Low:23.95 Close:24.19 Volume:58890}]

Index

Examples

Package Files

csvdecoding.go

type Decoder Uses

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

A Decoder reads and decodes CSV objects from an input stream.

func New Uses

func New(r io.Reader) *Decoder

New returns a new decoder that reads from r.

func (*Decoder) Decode Uses

func (d *Decoder) Decode(v interface{}) error

Decode reads the CSV-encoded value from its input and stores it in the value pointed to by v.

If a CSV value is not appropriate for a given type of a struct field, decode skips the field and tries to finish decoding as good as it can. If no more serious errors are encountered it returns an UnmarshalTypeError describing the first occurence of the error.

Decode capitalises CSV headers and maps them against struct field names. (E.g. columns named `foo` and `Foo` will be matched agains a struct field name `Foo`, the latter occurence overwrites the first one.)

type UnmarshalTypeError Uses

type UnmarshalTypeError struct {
    Value string
    Type  reflect.Type
}

UnmarshalTypeError will be returned from Decode when a given CSV value cannot be unmarshaled into a go value of a given type.

func (*UnmarshalTypeError) Error Uses

func (e *UnmarshalTypeError) Error() string

Package csvdecoding imports 7 packages (graph) and is imported by 1 packages. Updated 2016-07-29. Refresh now. Tools for package owners.