csv

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

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

Go to latest
Published: Nov 15, 2016 License: MIT Imports: 1 Imported by: 0

README

README

Build Status Godoc Reference

Warning: This library is still in alpha. API and behavior are expected to change.

Fast CSV reader library. Reads CSV files 5X faster than encoding/csv.

BenchmarkStdCsv-4   100   140391400 ns/op   15258771 B/op  696086 allocs/op
BenchmarkFastCsv-4  500    25303160 ns/op       2224 B/op       4 allocs/op

Differences from encoding/csv

  • This library is fast because it only handles standard delimiters (, and ") instead of the full gamut of UTF-8 runes accepted by the standard library implementation.
  • This implementation provides a streaming interface; only one row is valid at a time; subsequent calls to Read() invalidate the previously read row. This keeps allocs down to O(1) instead of O(N).
  • This implementation deals in []byte instead of string. This also helps to keep allocs down to O(1) instead of O(N).

Example

r := NewReader(csvFile)
for r.Next() {
    fmt.Println(string(bytes.Join(r.Fields(), []byte(", "))))
}
if err := r.Err(); err != nil {
    panic(err)
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

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

func NewReader

func NewReader(r io.Reader) Reader

Constructs a new Reader from a source CSV io.Reader

func (*Reader) Err

func (r *Reader) Err() error

Return the last error encountered; returns nil if no error was encountered or if the last error was io.EOF.

func (*Reader) Fields

func (r *Reader) Fields() [][]byte

Returns the last row of fields encountered. These fields are only valid until the next call to Next() or Read().

func (*Reader) Next

func (r *Reader) Next() bool

Scans in the next row

Example
r := NewReader(strings.NewReader(`Language,Sponsor
golang,Google
swift,Apple
rust,Mozilla`))
for r.Next() {
	fmt.Printf("[%s]\n", string(bytes.Join(r.Fields(), []byte(" | "))))
}
if err := r.Err(); err != nil {
	panic(err)
}
Output:

[Language | Sponsor]
[golang | Google]
[swift | Apple]
[rust | Mozilla]

func (*Reader) Read

func (r *Reader) Read() ([][]byte, error)

Read and return the next row and/or any errors encountered. The byte slices are only valid until the next call to Next() or Read(). Returns nil, io.EOF when the file is consumed.

Example
r := NewReader(strings.NewReader(`Language,Sponsor
golang,Google
swift,Apple
rust,Mozilla`))
for {
	row, err := r.Read()
	if err != nil {
		if err == io.EOF {
			break
		}
		panic(err)
	}

	fmt.Printf("[%s]\n", string(bytes.Join(row, []byte(" | "))))
}
Output:

[Language | Sponsor]
[golang | Google]
[swift | Apple]
[rust | Mozilla]

Jump to

Keyboard shortcuts

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