csvdecoding

package
v0.0.0-...-d7d9f52 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2014 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

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.

Example
package main

import (
	"fmt"
	"strings"
	"time"

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

func main() {
	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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

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

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

func New

func New(r io.Reader) *Decoder

New returns a new decoder that reads from r.

func (*Decoder) Decode

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

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

func (e *UnmarshalTypeError) Error() string

Jump to

Keyboard shortcuts

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