Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal the value v into a csv
Example ¶
package main import ( "fmt" "strconv" "strings" "time" "github.com/conradludgate/csv" ) type Data struct { Foo string Bar int64 `csv:"bar"` Time time.Time Custom Custom } type Custom struct { A string B int } func (c *Custom) UnmarshalCSV(value string) error { split := strings.Split(value, "|") if len(split) != 2 { return fmt.Errorf("invalid data for custom decode") } c.A = split[0] var err error c.B, err = strconv.Atoi(split[1]) return err } func (c Custom) MarshalCSV() string { return fmt.Sprintf("%s|%d", c.A, c.B) } func main() { time1 := time.Date(2006, 01, 02, 15, 04, 05, 0, time.FixedZone("MST", -7*60*60)) time2 := time.Date(2020, 07, 03, 16, 39, 44, 0, time.FixedZone("BST", 1*60*60)) data := []Data{ { Foo: "hello world", Bar: 9223372036854775807, Time: time1, Custom: Custom{ A: "value1", B: 1, }, }, { Foo: "goodbye world", Bar: -9223372036854775808, Time: time2, Custom: Custom{ A: "value2", B: 2, }, }, } bytes, _ := csv.Marshal(data) fmt.Println(string(bytes)) }
Output: Foo,bar,Time,Custom hello world,9223372036854775807,2006-01-02T15:04:05-07:00,value1|1 goodbye world,-9223372036854775808,2020-07-03T16:39:44+01:00,value2|2
func Unmarshal ¶
Unmarshal the byte slice as a csv into the value v v must be an array of structs, where the struct field names (or tags) define the csv header name to decode from Will decode most built in types automatically, otherwise it will use the FromString interface to decode
Example ¶
package main import ( "encoding/json" "fmt" "strconv" "strings" "time" "github.com/conradludgate/csv" ) type Data struct { Foo string Bar int64 `csv:"bar"` Time time.Time Custom Custom } type Custom struct { A string B int } func (c *Custom) UnmarshalCSV(value string) error { split := strings.Split(value, "|") if len(split) != 2 { return fmt.Errorf("invalid data for custom decode") } c.A = split[0] var err error c.B, err = strconv.Atoi(split[1]) return err } func (c Custom) MarshalCSV() string { return fmt.Sprintf("%s|%d", c.A, c.B) } func main() { data := []byte(`Foo,bar,Time,Custom hello world,9223372036854775807,2006-01-02T15:04:05-07:00,value1|1 goodbye world,-9223372036854775808,2020-07-03T16:39:44+01:00,value2|2`) output := []Data{} err := csv.Unmarshal(data, &output) if err != nil { panic(err) } outputJSON, _ := json.MarshalIndent(output, "", "\t") fmt.Println(string(outputJSON)) }
Output: [ { "Foo": "hello world", "Bar": 9223372036854775807, "Time": "2006-01-02T15:04:05-07:00", "Custom": { "A": "value1", "B": 1 } }, { "Foo": "goodbye world", "Bar": -9223372036854775808, "Time": "2020-07-03T16:39:44+01:00", "Custom": { "A": "value2", "B": 2 } } ]
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder reads and decodes a csv into an array from an input stream
func NewDecoder ¶
NewDecoder creates a new decoder from the given reader
func (*Decoder) Decode ¶
Decode decodes the reader into the value v v must be an array of structs, where the struct field names (or tags) define the csv header name to decode from Will decode most built in types, otherwise it will use the FromString interface to decode
Example ¶
package main import ( "encoding/json" "fmt" "strconv" "strings" "time" "github.com/conradludgate/csv" ) type Data struct { Foo string Bar int64 `csv:"bar"` Time time.Time Custom Custom } type Custom struct { A string B int } func (c *Custom) UnmarshalCSV(value string) error { split := strings.Split(value, "|") if len(split) != 2 { return fmt.Errorf("invalid data for custom decode") } c.A = split[0] var err error c.B, err = strconv.Atoi(split[1]) return err } func (c Custom) MarshalCSV() string { return fmt.Sprintf("%s|%d", c.A, c.B) } func main() { data := strings.NewReader(`Foo,bar,Time,Custom hello world,9223372036854775807,2006-01-02T15:04:05-07:00,value1|1 goodbye world,-9223372036854775808,2020-07-03T16:39:44+01:00,value2|2`) decoder := csv.NewDecoder(data) output := []Data{} err := decoder.Decode(&output) if err != nil { panic(err) } outputJSON, _ := json.MarshalIndent(output, "", "\t") fmt.Println(string(outputJSON)) }
Output: [ { "Foo": "hello world", "Bar": 9223372036854775807, "Time": "2006-01-02T15:04:05-07:00", "Custom": { "A": "value1", "B": 1 } }, { "Foo": "goodbye world", "Bar": -9223372036854775808, "Time": "2020-07-03T16:39:44+01:00", "Custom": { "A": "value2", "B": 2 } } ]
func (*Decoder) DisableTrimLeadingSpace ¶
func (d *Decoder) DisableTrimLeadingSpace()
DisableTrimLeadingSpace sets the value for the underlying reader to false. If TrimLeadingSpace is true, leading white space in a field is ignored. This is done even if the field delimiter is white space.
Example ¶
package main import ( "encoding/json" "fmt" "strconv" "strings" "time" "github.com/conradludgate/csv" ) type Data struct { Foo string Bar int64 `csv:"bar"` Time time.Time Custom Custom } type Custom struct { A string B int } func (c *Custom) UnmarshalCSV(value string) error { split := strings.Split(value, "|") if len(split) != 2 { return fmt.Errorf("invalid data for custom decode") } c.A = split[0] var err error c.B, err = strconv.Atoi(split[1]) return err } func (c Custom) MarshalCSV() string { return fmt.Sprintf("%s|%d", c.A, c.B) } func main() { data := strings.NewReader(`Foo,bar,Time,Custom hello world,9223372036854775807,2006-01-02T15:04:05-07:00,value1|1 goodbye world,-9223372036854775808,2020-07-03T16:39:44+01:00,value2|2`) decoder := csv.NewDecoder(data) decoder.DisableTrimLeadingSpace() output := []Data{} err := decoder.Decode(&output) if err != nil { panic(err) } outputJSON, _ := json.MarshalIndent(output, "", "\t") fmt.Println(string(outputJSON)) }
Output: [ { "Foo": " hello world", "Bar": 9223372036854775807, "Time": "2006-01-02T15:04:05-07:00", "Custom": { "A": "value1", "B": 1 } }, { "Foo": "goodbye world", "Bar": -9223372036854775808, "Time": "2020-07-03T16:39:44+01:00", "Custom": { "A": "value2", "B": 2 } } ]
func (*Decoder) SetComment ¶
SetComment character for the csv reader
Example ¶
package main import ( "encoding/json" "fmt" "strconv" "strings" "time" "github.com/conradludgate/csv" ) type Data struct { Foo string Bar int64 `csv:"bar"` Time time.Time Custom Custom } type Custom struct { A string B int } func (c *Custom) UnmarshalCSV(value string) error { split := strings.Split(value, "|") if len(split) != 2 { return fmt.Errorf("invalid data for custom decode") } c.A = split[0] var err error c.B, err = strconv.Atoi(split[1]) return err } func (c Custom) MarshalCSV() string { return fmt.Sprintf("%s|%d", c.A, c.B) } func main() { data := strings.NewReader(`Foo,bar,Time,Custom hello world,9223372036854775807,2006-01-02T15:04:05-07:00,value1|1 # This line is a comment and will be ignored goodbye world,-9223372036854775808,2020-07-03T16:39:44+01:00,value2|2`) decoder := csv.NewDecoder(data) decoder.SetComment('#') output := []Data{} err := decoder.Decode(&output) if err != nil { panic(err) } outputJSON, _ := json.MarshalIndent(output, "", "\t") fmt.Println(string(outputJSON)) }
Output: [ { "Foo": "hello world", "Bar": 9223372036854775807, "Time": "2006-01-02T15:04:05-07:00", "Custom": { "A": "value1", "B": 1 } }, { "Foo": "goodbye world", "Bar": -9223372036854775808, "Time": "2020-07-03T16:39:44+01:00", "Custom": { "A": "value2", "B": 2 } } ]
func (*Decoder) SetDelimiter ¶
SetDelimiter character for the csv reader
Example ¶
package main import ( "encoding/json" "fmt" "strconv" "strings" "time" "github.com/conradludgate/csv" ) type Data struct { Foo string Bar int64 `csv:"bar"` Time time.Time Custom Custom } type Custom struct { A string B int } func (c *Custom) UnmarshalCSV(value string) error { split := strings.Split(value, "|") if len(split) != 2 { return fmt.Errorf("invalid data for custom decode") } c.A = split[0] var err error c.B, err = strconv.Atoi(split[1]) return err } func (c Custom) MarshalCSV() string { return fmt.Sprintf("%s|%d", c.A, c.B) } func main() { data := strings.NewReader(`Foo~bar~Time~Custom hello world~9223372036854775807~2006-01-02T15:04:05-07:00~value1|1 goodbye world~-9223372036854775808~2020-07-03T16:39:44+01:00~value2|2`) decoder := csv.NewDecoder(data) decoder.SetDelimiter('~') output := []Data{} err := decoder.Decode(&output) if err != nil { panic(err) } outputJSON, _ := json.MarshalIndent(output, "", "\t") fmt.Println(string(outputJSON)) }
Output: [ { "Foo": "hello world", "Bar": 9223372036854775807, "Time": "2006-01-02T15:04:05-07:00", "Custom": { "A": "value1", "B": 1 } }, { "Foo": "goodbye world", "Bar": -9223372036854775808, "Time": "2020-07-03T16:39:44+01:00", "Custom": { "A": "value2", "B": 2 } } ]
func (*Decoder) TrimLeadingSpace ¶
func (d *Decoder) TrimLeadingSpace()
TrimLeadingSpace the value for the underlying reader to true. If TrimLeadingSpace is true, leading white space in a field is ignored. This is done even if the field delimiter is white space.
Example ¶
package main import ( "encoding/json" "fmt" "strconv" "strings" "time" "github.com/conradludgate/csv" ) type Data struct { Foo string Bar int64 `csv:"bar"` Time time.Time Custom Custom } type Custom struct { A string B int } func (c *Custom) UnmarshalCSV(value string) error { split := strings.Split(value, "|") if len(split) != 2 { return fmt.Errorf("invalid data for custom decode") } c.A = split[0] var err error c.B, err = strconv.Atoi(split[1]) return err } func (c Custom) MarshalCSV() string { return fmt.Sprintf("%s|%d", c.A, c.B) } func main() { data := strings.NewReader(` Foo, bar, Time, Custom hello world, 9223372036854775807,2006-01-02T15:04:05-07:00,value1|1 goodbye world,-9223372036854775808,2020-07-03T16:39:44+01:00,value2|2`) decoder := csv.NewDecoder(data) decoder.TrimLeadingSpace() output := []Data{} err := decoder.Decode(&output) if err != nil { panic(err) } outputJSON, _ := json.MarshalIndent(output, "", "\t") fmt.Println(string(outputJSON)) }
Output: [ { "Foo": "hello world", "Bar": 9223372036854775807, "Time": "2006-01-02T15:04:05-07:00", "Custom": { "A": "value1", "B": 1 } }, { "Foo": "goodbye world", "Bar": -9223372036854775808, "Time": "2020-07-03T16:39:44+01:00", "Custom": { "A": "value2", "B": 2 } } ]
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder encodes and writes the contents of a slice into a csv file
func NewEncoder ¶
NewEncoder creates a new encoder from the given writer
func (*Encoder) Encode ¶
Encode and write the value of v into a csv
Example ¶
package main import ( "fmt" "os" "strconv" "strings" "time" "github.com/conradludgate/csv" ) type Data struct { Foo string Bar int64 `csv:"bar"` Time time.Time Custom Custom } type Custom struct { A string B int } func (c *Custom) UnmarshalCSV(value string) error { split := strings.Split(value, "|") if len(split) != 2 { return fmt.Errorf("invalid data for custom decode") } c.A = split[0] var err error c.B, err = strconv.Atoi(split[1]) return err } func (c Custom) MarshalCSV() string { return fmt.Sprintf("%s|%d", c.A, c.B) } func main() { time1 := time.Date(2006, 01, 02, 15, 04, 05, 0, time.FixedZone("MST", -7*60*60)) time2 := time.Date(2020, 07, 03, 16, 39, 44, 0, time.FixedZone("BST", 1*60*60)) data := []Data{ { Foo: "hello world", Bar: 9223372036854775807, Time: time1, Custom: Custom{ A: "value1", B: 1, }, }, { Foo: "goodbye world", Bar: -9223372036854775808, Time: time2, Custom: Custom{ A: "value2", B: 2, }, }, } encoder := csv.NewEncoder(os.Stdout) encoder.Encode(data) }
Output: Foo,bar,Time,Custom hello world,9223372036854775807,2006-01-02T15:04:05-07:00,value1|1 goodbye world,-9223372036854775808,2020-07-03T16:39:44+01:00,value2|2
func (*Encoder) NoCRLF ¶
func (e *Encoder) NoCRLF()
NoCRLF disables CRLF line ending for the csv Writer
func (*Encoder) SetDelimiter ¶
SetDelimiter character for the csv reader
Example ¶
package main import ( "fmt" "os" "strconv" "strings" "time" "github.com/conradludgate/csv" ) type Data struct { Foo string Bar int64 `csv:"bar"` Time time.Time Custom Custom } type Custom struct { A string B int } func (c *Custom) UnmarshalCSV(value string) error { split := strings.Split(value, "|") if len(split) != 2 { return fmt.Errorf("invalid data for custom decode") } c.A = split[0] var err error c.B, err = strconv.Atoi(split[1]) return err } func (c Custom) MarshalCSV() string { return fmt.Sprintf("%s|%d", c.A, c.B) } func main() { time1 := time.Date(2006, 01, 02, 15, 04, 05, 0, time.FixedZone("MST", -7*60*60)) time2 := time.Date(2020, 07, 03, 16, 39, 44, 0, time.FixedZone("BST", 1*60*60)) data := []Data{ { Foo: "hello world", Bar: 9223372036854775807, Time: time1, Custom: Custom{ A: "value1", B: 1, }, }, { Foo: "goodbye world", Bar: -9223372036854775808, Time: time2, Custom: Custom{ A: "value2", B: 2, }, }, } encoder := csv.NewEncoder(os.Stdout) encoder.SetDelimiter('~') encoder.Encode(data) }
Output: Foo~bar~Time~Custom hello world~9223372036854775807~2006-01-02T15:04:05-07:00~value1|1 goodbye world~-9223372036854775808~2020-07-03T16:39:44+01:00~value2|2
type MarshalCSV ¶
type MarshalCSV interface {
MarshalCSV() string
}
MarshalCSV describes how CSV should handle types that aren't strings or built in
type UnmarshalCSV ¶
UnmarshalCSV describes how CSV should handle types that aren't strings or built in