csvstruct

package module
v0.0.0-...-cb8ca48 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2015 License: Apache-2.0 Imports: 9 Imported by: 0

README

GoDoc Build Status

This library provides methods to read and write CSV data into and from Go structs.

Decoding

Given the following CSV data:

Name,Age,Height
Alice,25,5.7
Bob,24,5.9

You could decode the data into structs like so:

f, _ := os.Open("path/to/your.csv")
defer f.Close()
type Person struct {
	Name string
	Age int
	Height float64
}
var p Person
d := csvstruct.NewDecoder(f)
for {
	if err := d.DecodeNext(&p); err == io.EOF {
		break
	} else if err != nil {
		// handle error
	}
	fmt.Printf('%s's age is %d\n", p.Name, p.Age)
}

Encoding

Similarly, given structs, you can generate CSV data.

people := []Person{{"Carl", 31, 6.0}, {"Debbie", 27, 5.3}}
e := csvstruct.NewEncoder(os.Stdout)
for _, p := range people {
	if err := e.EncodeNext(p); err != nil {
		// handle error
	}
}

Struct tags are supported to override the struct's field names and ignore fields. See the GoDoc for more information and tests for more examples.


License

Copyright 2014 Jason Hall

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package csvstruct provides methods to decode a CSV file into a struct.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DecodeOpts

type DecodeOpts struct {
	Comma            rune // field delimiter (set to ',' by default)
	Comment          rune // comment character for start of line
	LazyQuotes       bool // allow lazy quotes
	TrimLeadingSpace bool // trim leading space
}

DecodeOpts specifies options to modify decoding behavior.

type Decoder

type Decoder interface {
	// DecodeNext populates v with the values from the next row in the
	// Decoder's Reader.
	//
	// On the first call to DecodeNext, the first row in the reader will be
	// used as the header row to map CSV fields to struct fields, and the
	// second row will be read to populate v.
	DecodeNext(v interface{}) error

	// Opts specifies options to modify decoding behavior.
	//
	// It returns the Decoder, to support chaining.
	Opts(DecodeOpts) Decoder
}

Decoder reads and decodes CSV rows from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) Decoder

NewDecoder returns a Decoder that reads from r.

type EncodeOpts

type EncodeOpts struct {
	SkipHeader bool // True to skip writing the header row
	Comma      rune // Field delimiter (set to ',' by default)
	UseCRLF    bool // True to use \r\n as the line terminator
}

EncodeOpts specifies options to modify encoding behavior.

type Encoder

type Encoder interface {
	// EncodeNext encodes v into a CSV row and writes it to the Encoder's
	// Writer.
	//
	// On the first call to EncodeNext, v's fields will be used to write the
	// header row, then v's values will be written as the second row.
	EncodeNext(v interface{}) error

	// Opts specifies options to modify encoding behavior.
	//
	// It returns the Encoder, to support chaining.
	Opts(EncodeOpts) Encoder
}

Encoder encodes and writes CSV rows to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) Encoder

NewEncoder returns an encoder that writes to w.

Jump to

Keyboard shortcuts

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