csv

package
v0.1.361 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package csv implements a decoder, that supports CSV decoding. The decoding is guided by struct tags, similar to the use of struct tags in JSON or XML decoding.

Given an CSV file with a header row like this:

id        name
007       Seven

We can decode the CSV into an Item with a few lines:

import stdcsv "encoding/csv"

...

type Item struct {
    ID   string `csv:"id"`
    Name string `csv:"name"`
}

...

func main() {
    dec := csv.NewDecoder(stdcsv.NewReader(os.Stdin))
    var item Item
    if err := dec.Decode(&item); err != nil {
        log.Fatal(err)
    }
    fmt.Println(item.ID, item.Name) // 007 Seven
}

Since the decoder takes a csv.Reader as argument, you can customize any CSV related property like comma or number of fields on that reader.

Missing fields are ignored. Only string fields are supported.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder struct {
	Header []string // Column names.
	// contains filtered or unexported fields
}

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

func NewDecoder

func NewDecoder(c *stdcsv.Reader) *Decoder

NewDecoder returns a new decoder.

func (*Decoder) Decode

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

Decode a single entry, use csv struct tags.

Jump to

Keyboard shortcuts

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