go-csv: github.com/JensRantil/go-csv Index | Examples | Files | Directories

package csv

import "github.com/JensRantil/go-csv"

A CSV implementation inspired by Python's CSV module. Supports custom CSV formats.

Code:

buf := bytes.Buffer{}

writer := csv.NewWriter(&buf)
writer.Write([]string{"Hello", "World", "!"})
writer.Flush()

reader := csv.NewReader(&buf)
columns, err := reader.Read()
if err != nil {
    panic(err)
}

for _, s := range columns {
    fmt.Println(s)
}

Output:

Hello
World
!

Index

Examples

Package Files

common.go reader.go writer.go

Constants

const (
    QuoteDefault    QuoteMode = iota // See DefaultQuoting.
    QuoteAll                  = iota // Quotes around every field.
    QuoteMinimal              = iota // Quotes when needed.
    QuoteNonNumeric           = iota // Quotes around non-numeric fields.

    // Never quote. Use with care. Could make things unparsable.
    QuoteNone = iota
)

Values QuoteMode can take.

const (
    DoubleQuoteDefault DoubleQuoteMode = iota // See DefaultDoubleQuote.
    DoDoubleQuote                      = iota // Escape using double escape characters.
    NoDoubleQuote                      = iota // Escape using escape character.
)

Values DoubleQuoteMode can take.

const (
    DefaultDelimiter      = ','
    DefaultQuoting        = QuoteMinimal
    DefaultDoubleQuote    = DoDoubleQuote
    DefaultEscapeChar     = '\\'
    DefaultQuoteChar      = '"'
    DefaultLineTerminator = "\n"
)

Default dialect.

type Dialect Uses

type Dialect struct {
    // The delimiter that separates each field from another. Defaults to
    // DefaultDelimiter.
    Delimiter rune
    // What quoting mode to use. Defaults to DefaultQuoting.
    Quoting QuoteMode
    // How to escape quotes. Defaults to DefaultDoubleQuote.
    DoubleQuote DoubleQuoteMode
    // Character to use for escaping. Only used if DoubleQuote==NoDoubleQuote.
    // Defaults to DefaultEscapeChar.
    EscapeChar rune
    // Character to use as quotation mark around quoted fields. Defaults to
    // DefaultQuoteChar.
    QuoteChar rune
    // String that separates each record in a CSV file. Defaults to
    // DefaultLineTerminator.
    LineTerminator string
}

A Dialect specifies the format of a CSV file. This structure is used by a Reader or Writer to know how to operate on the file they are reading/writing.

type DoubleQuoteMode Uses

type DoubleQuoteMode int

DoubleQuoteMode defined how quote excaping should be done.

type QuoteMode Uses

type QuoteMode int

QuoteMode defines how quotes should be handled.

type Reader Uses

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

A Reader reads records from a CSV-encoded file.

Can be created by calling either NewReader or using NewDialectReader.

func NewDialectReader Uses

func NewDialectReader(r io.Reader, opts Dialect) *Reader

Create a custom CSV reader.

func NewReader Uses

func NewReader(r io.Reader) *Reader

Creates a reader that conforms to RFC 4180 and behaves identical as a encoding/csv.Reader.

See `Default*` constants for default dialect used.

func (*Reader) Read Uses

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

Read reads one record from r. The record is a slice of strings with each string representing one field.

func (*Reader) ReadAll Uses

func (r *Reader) ReadAll() ([][]string, error)

ReadAll reads all the remaining records from r. Each record is a slice of fields. A successful call returns err == nil, not err == EOF. Because ReadAll is defined to read until EOF, it does not treat end of file as an error to be reported.

type Writer Uses

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

A Writer writes records to a CSV encoded file.

Can be created by calling either NewWriter or using NewDialectWriter.

func NewDialectWriter Uses

func NewDialectWriter(w io.Writer, opts Dialect) Writer

Create a custom CSV writer.

func NewWriter Uses

func NewWriter(w io.Writer) Writer

Create a writer that conforms to RFC 4180 and behaves identical as a encoding/csv.Reader.

See `Default*` constants for default dialect used.

func (Writer) Error Uses

func (w Writer) Error() error

Error reports any error that has occurred during a previous Write or Flush.

func (Writer) Flush Uses

func (w Writer) Flush()

Flush writes any buffered data to the underlying io.Writer. To check if an error occurred during the Flush, call Error.

func (Writer) Write Uses

func (w Writer) Write(record []string) (err error)

Writer writes a single CSV record to w along with any necessary quoting. A record is a slice of strings with each string being one field.

func (Writer) WriteAll Uses

func (w Writer) WriteAll(records [][]string) (err error)

WriteAll writes multiple CSV records to w using Write and then calls Flush.

Directories

PathSynopsis
dialectHelpers that makes it easy to build CSV dialects.
interfacesInterfaces shared among go-csv and the Go standard library's encoding/csv.

Package csv imports 6 packages (graph) and is imported by 4 packages. Updated 2019-08-15. Refresh now. Tools for package owners.