headercsv

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 25, 2022 License: MIT Imports: 10 Imported by: 0

README

test PkgGoDev

SYNOPSIS

go-header-csv is encoder/decoder csv with a header.

The following is an example of csv.

func ExampleDecoder_DecodeAll() {
	in := `name,text
Ed,Knock knock.
Sam,Who's there?
Ed,Go fmt.
Sam,Go fmt who?
Ed,Go fmt yourself!
`
	out := []struct {
		Name string `csv:"name"`
		Text string `csv:"text"`
	}{}

	buf := bytes.NewBufferString(in)
	dec := headercsv.NewDecoder(buf)
	dec.Decode(&out)

	for _, v := range out {
		fmt.Printf("%3s: %s\n", v.Name, v.Text)
	}
	// Output:
	//  Ed: Knock knock.
	// Sam: Who's there?
	//  Ed: Go fmt.
	// Sam: Go fmt who?
	//  Ed: Go fmt yourself!
}
func ExampleEncoder_EncodeAll() {
	in := []struct {
		Name string `csv:"name"`
		Text string `csv:"text"`
	}{
		{"Ed", "Knock knock."},
		{"Sam", "Who's there?"},
		{"Ed", "Go fmt."},
		{"Sam", "Go fmt who?"},
		{"Ed", "Go fmt yourself!"},
	}

	enc := headercsv.NewEncoder(os.Stdout)
	enc.EncodeAll(in)
	enc.Flush()

	// Output:
	// name,text
	// Ed,Knock knock.
	// Sam,Who's there?
	// Ed,Go fmt.
	// Sam,Go fmt who?
	// Ed,Go fmt yourself!
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder struct {
	UnmarshalField func(in []byte, out any) error
	// contains filtered or unexported fields
}

Decoder reads and decodes CSV values from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

func NewDecoderCSV added in v0.0.2

func NewDecoderCSV(r *csv.Reader) *Decoder

NewDecoderCSV returns a new decoder that reads from r.

func (*Decoder) Decode deprecated

func (dec *Decoder) Decode(v any) error

Decode reads the next CSV record from its input and stores it in the value pointed to by v.

Deprecated: use DecodeRecord or DecodeAll instead.

Example
package main

import (
	"bytes"
	"fmt"

	headercsv "github.com/shogo82148/go-header-csv"
)

func main() {
	in := `name,text
Ed,Knock knock.
Sam,Who's there?
Ed,Go fmt.
Sam,Go fmt who?
Ed,Go fmt yourself!
`
	out := []struct {
		Name string `csv:"name"`
		Text string `csv:"text"`
	}{}

	buf := bytes.NewBufferString(in)
	dec := headercsv.NewDecoder(buf)
	dec.Decode(&out)

	for _, v := range out {
		fmt.Printf("%3s: %s\n", v.Name, v.Text)
	}
}
Output:

 Ed: Knock knock.
Sam: Who's there?
 Ed: Go fmt.
Sam: Go fmt who?
 Ed: Go fmt yourself!

func (*Decoder) DecodeAll added in v0.0.2

func (dec *Decoder) DecodeAll(v any) error

DecodeAll reads all CSV record from its input. v must be a pinter to a slice or a pointer to an array.

Example
package main

import (
	"bytes"
	"fmt"

	headercsv "github.com/shogo82148/go-header-csv"
)

func main() {
	in := `name,text
Ed,Knock knock.
Sam,Who's there?
Ed,Go fmt.
Sam,Go fmt who?
Ed,Go fmt yourself!
`
	out := []struct {
		Name string `csv:"name"`
		Text string `csv:"text"`
	}{}

	buf := bytes.NewBufferString(in)
	dec := headercsv.NewDecoder(buf)
	dec.Decode(&out)

	for _, v := range out {
		fmt.Printf("%3s: %s\n", v.Name, v.Text)
	}
}
Output:

 Ed: Knock knock.
Sam: Who's there?
 Ed: Go fmt.
Sam: Go fmt who?
 Ed: Go fmt yourself!

func (*Decoder) DecodeRecord added in v0.0.2

func (dec *Decoder) DecodeRecord(v any) error

DecodeRecord reads the next CSV record from its input and stores it in the value pointed to by v.

Example
package main

import (
	"bytes"
	"fmt"

	headercsv "github.com/shogo82148/go-header-csv"
)

func main() {
	in := `name,text
Ed,Knock knock.
Sam,Who's there?
Ed,Go fmt.
Sam,Go fmt who?
Ed,Go fmt yourself!
`
	out := struct {
		Name string `csv:"name"`
		Text string `csv:"text"`
	}{}

	buf := bytes.NewBufferString(in)
	dec := headercsv.NewDecoder(buf)
	dec.DecodeRecord(&out)

	fmt.Printf("%3s: %s\n", out.Name, out.Text)
}
Output:

 Ed: Knock knock.

func (*Decoder) SetHeader

func (dec *Decoder) SetHeader(header []string) error

SetHeader sets the header. If no header is set, first CSV record is used for the header.

type Encoder

type Encoder struct {
	MarshalField func(v any) ([]byte, error)
	// contains filtered or unexported fields
}

Encoder writes CSV records to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func NewEncoderCSV added in v0.0.2

func NewEncoderCSV(w *csv.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode deprecated

func (enc *Encoder) Encode(v any) error

Encode writes a CSV record to the stream.

Deprecated: use EncodeRecord or EncodeAll instead.

Example
package main

import (
	"os"

	headercsv "github.com/shogo82148/go-header-csv"
)

func main() {
	in := []struct {
		Name string `csv:"name"`
		Text string `csv:"text"`
	}{
		{"Ed", "Knock knock."},
		{"Sam", "Who's there?"},
		{"Ed", "Go fmt."},
		{"Sam", "Go fmt who?"},
		{"Ed", "Go fmt yourself!"},
	}

	enc := headercsv.NewEncoder(os.Stdout)
	enc.Encode(in)
	enc.Flush()

}
Output:

name,text
Ed,Knock knock.
Sam,Who's there?
Ed,Go fmt.
Sam,Go fmt who?
Ed,Go fmt yourself!

func (*Encoder) EncodeAll added in v0.0.2

func (enc *Encoder) EncodeAll(v any) error

EncodeAll writes all CSV records to the stream. v must be a slice or an array.

Example
package main

import (
	"os"

	headercsv "github.com/shogo82148/go-header-csv"
)

func main() {
	in := []struct {
		Name string `csv:"name"`
		Text string `csv:"text"`
	}{
		{"Ed", "Knock knock."},
		{"Sam", "Who's there?"},
		{"Ed", "Go fmt."},
		{"Sam", "Go fmt who?"},
		{"Ed", "Go fmt yourself!"},
	}

	enc := headercsv.NewEncoder(os.Stdout)
	enc.EncodeAll(in)
	enc.Flush()

}
Output:

name,text
Ed,Knock knock.
Sam,Who's there?
Ed,Go fmt.
Sam,Go fmt who?
Ed,Go fmt yourself!

func (*Encoder) EncodeRecord added in v0.0.2

func (enc *Encoder) EncodeRecord(v any) error

EncodeRecord writes a CSV record to the stream.

Example
package main

import (
	"os"

	headercsv "github.com/shogo82148/go-header-csv"
)

func main() {
	in := struct {
		Name string `csv:"name"`
		Text string `csv:"text"`
	}{"Ed", "Knock knock."}

	enc := headercsv.NewEncoder(os.Stdout)
	enc.EncodeRecord(in)
	enc.Flush()

}
Output:

name,text
Ed,Knock knock.

func (*Encoder) Error added in v0.0.2

func (enc *Encoder) Error() error

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

func (*Encoder) Flush

func (enc *Encoder) Flush()

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

func (*Encoder) SetHeader

func (enc *Encoder) SetHeader(header []string) error

SetHeader sets the header.

Jump to

Keyboard shortcuts

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