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 ¶
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" 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
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
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.
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) 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
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
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
Error reports any error that has occurred during a previous Write or Flush.