Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DecodeError ¶ added in v0.1.0
type DecodeError struct {
StartLine int // Line where the record starts
Line int // Line where the error occurred
Column int // Column (1-based byte index) where the error occurred
Field string // Field name where the error occurred
Err error // The actual error
}
A DecodeError is returned for decoding errors. Line and column numbers are 1-indexed.
func (*DecodeError) Error ¶ added in v0.1.0
func (e *DecodeError) Error() string
func (*DecodeError) Unwrap ¶ added in v0.1.0
func (e *DecodeError) Unwrap() error
Unwrap returns the underlying error.
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 ¶
NewDecoder returns a new decoder that reads from r.
func NewDecoderCSV ¶ added in v0.0.2
NewDecoderCSV returns a new decoder that reads from r.
func (*Decoder) Decode
deprecated
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"
"io"
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)
if err := dec.Decode(&out); err != nil && err != io.EOF {
panic(err)
}
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
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)
if err := dec.DecodeAll(&out); err != nil {
panic(err)
}
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
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)
if err := dec.DecodeRecord(&out); err != nil {
panic(err)
}
fmt.Printf("%3s: %s\n", out.Name, out.Text)
}
Output: Ed: Knock knock.
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 ¶
NewEncoder returns a new encoder that writes to w.
func NewEncoderCSV ¶ added in v0.0.2
NewEncoder returns a new encoder that writes to w.
func (*Encoder) Encode
deprecated
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)
if err := enc.Encode(in); err != nil {
panic(err)
}
enc.Flush()
if err := enc.Error(); err != nil {
panic(err)
}
}
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
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)
if err := enc.EncodeAll(in); err != nil {
panic(err)
}
enc.Flush()
if err := enc.Error(); err != nil {
panic(err)
}
}
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
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)
if err := enc.EncodeRecord(in); err != nil {
panic(err)
}
enc.Flush()
if err := enc.Error(); err != nil {
panic(err)
}
}
Output: name,text Ed,Knock knock.
func (*Encoder) Error ¶ added in v0.0.2
Error reports any error that has occurred during a previous Write or Flush.