Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"log"
"github.com/KalleDK/go-csv/csv"
)
type Record struct {
Name string
Age int
}
var examplecsv = []byte(`"Name","Age"
"Bob","12"
"Sally","13"
"Alice","10"
`)
func main() {
var records []Record
if err := csv.Unmarshal(&records, nil, examplecsv); err != nil {
log.Fatal(err)
}
fmt.Println(records)
}
Output: [{Bob 12} {Sally 13} {Alice 10}]
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Unmarshal ¶
Unmarshal parses the CSV-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.
Example ¶
package main
import (
"fmt"
"log"
"github.com/KalleDK/go-csv/csv"
)
type SimpleRecord struct {
Name string
Age int
}
var simplecsv = []byte(`"Name","Age"
"Bob","12"
"Sally","13"
"Alice","10"
`)
func main() {
var records []SimpleRecord
if err := csv.Unmarshal(&records, nil, simplecsv); err != nil {
log.Fatal(err)
}
fmt.Println(records)
}
Output: [{Bob 12} {Sally 13} {Alice 10}]
Example (Advanced) ¶
package main
import (
"fmt"
"log"
"github.com/KalleDK/go-csv/csv"
)
type CSVField bool
func (f *CSVField) UnmarshalText(text []byte) error {
*f = (CSVField)(string(text) == "true")
return nil
}
type CSVRecord struct {
SimpleNumber int
SimpleString string
CustomMethod bool `csv:",DecodeThird"`
TextUnmarshal bool
OtherName string `csv:"RealName"`
}
func (r *CSVRecord) DecodeThird(v *bool, text []byte) error {
*v = "true" == string(text)
return nil
}
var advancedcsv = []byte(`"SimpleNumber","SimpleString","CustomMethod","TextUnmarshal","RealName"
"2","Bob","true","true","Sir Bob"
"3","Sally","false","false","Miss Alice"`)
func main() {
var records []CSVRecord
if err := csv.Unmarshal(&records, nil, advancedcsv); err != nil {
log.Fatal(err)
}
fmt.Println(records)
}
Output: [{2 Bob true true Sir Bob} {3 Sally false false Miss Alice}]
Example (Custom) ¶
package main
import (
"fmt"
"log"
"github.com/KalleDK/go-csv/csv"
)
type CustomRecord struct {
SimpleNumber int
SimpleString string
CustomMethod bool `csv:",DecodeBool"` // Telling the decoder to use the method DecodeBool to decode this bool
}
func (r *CustomRecord) DecodeBool(v *bool, text []byte) error {
*v = "true" == string(text)
return nil
}
var customcsv = []byte(`"SimpleNumber","SimpleString","CustomMethod"
"2","Bob","true"
"3","Sally","false"`)
func main() {
var records []CustomRecord
if err := csv.Unmarshal(&records, nil, customcsv); err != nil {
log.Fatal(err)
}
fmt.Println(records)
}
Output: [{2 Bob true} {3 Sally false}]
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder reads and decodes CSV values from an input stream.
func NewDecoder ¶
NewDecoder returns a new decoder that reads from r.
The decoder introduces its own buffering and may read data from r beyond the CSV values requested.
If headers is nil the headers are expected to be in the first csv record
type Options ¶
type Options struct {
// Headers is for mapping the Struct's fieldnames to columns
// If Headers are nil, the first record is expected to be headers
Headers []string
// Comma is the field delimiter.
// It is set to comma (',') by NewDecoder.
// Comma must be a valid rune and must not be \r, \n,
// or the Unicode replacement character (0xFFFD).
Comma rune
// Comment, if not 0, is the comment character. Lines beginning with the
// Comment character without preceding whitespace are ignored.
// With leading whitespace the Comment character becomes part of the
// field, even if TrimLeadingSpace is true.
// Comment must be a valid rune and must not be \r, \n,
// or the Unicode replacement character (0xFFFD).
// It must also not be equal to Comma.
Comment rune
// FieldsPerRecord is the number of expected fields per record.
// If FieldsPerRecord is positive, Read requires each record to
// have the given number of fields. If FieldsPerRecord is 0, Read sets it to
// the number of fields in the first record, so that future records must
// have the same field count. If FieldsPerRecord is negative, no check is
// made and records may have a variable number of fields.
FieldsPerRecord int
// If LazyQuotes is true, a quote may appear in an unquoted field and a
// non-doubled quote may appear in a quoted field.
LazyQuotes bool
// If TrimLeadingSpace is true, leading white space in a field is ignored.
// This is done even if the field delimiter, Comma, is white space.
TrimLeadingSpace bool
}
Options for how the CSV file is encoded
type UnmarshalFunc ¶
UnmarshalFunc is the method implemented by an object that can unmarshal a textual representation of the v.
UnmarshalFunc must be able to decode the form generated by MarshalFunc. UnmarshalFunc must copy the text if it wishes to retain the text after returning.