Provides tools to help set Go structures from CSV lines / files and vice versa.
To install assertions run:
$ go get github.com/rzajac/csvutil
Here we are setting person fields from CSV. The columns in CSV line must be in the same order as fields in the structure (from top to bottom). You may skip fields by tagging them with csv:"-"
.
import (
"github.com/rzajac/csvutil"
)
var testCsvLines = []string{"Tony|23|123.456|Y", "John|34|234.567|N|"}
type person struct {
Name string
Age int
Skipped string `csv:"-"` // Skip this field when setting the structure
Balance float32
LowBalance bool
}
main () {
// This can be any io.ReadCloser() interface
sr := csvutil.StringReadCloser(strings.Join(testCsvLines, "\n"))
// Set delimiter to '|', allow for trailing comma and do not check fields per CSV record
c := csvutil.NewCsvUtil(sr).Comma('|').TrailingComma(true).FieldsPerRecord(-1).CustomBool([]string{"Y"}, []string{"N"})
// Struct we will populate with CSV data
p := &person{Skipped: "aaa"}
// Set values from CSV line to person structure
err := c.SetData(p)
// Do work with p
// Set values from the second CSV line
err := c.SetData(p)
}
type person2 struct {
Name string
Balance float32
}
main () {
sr := csvutil.StringReadCloser(strings.Join(testCsvLines, "\n"))
// Set delimiter to '|', allow for trailing comma and do not check fields per CSV record
c := csvutil.NewCsvUtil(sr).Comma('|').TrailingComma(true).FieldsPerRecord(-1)
// Set header with column names matching structure fields and column indexes on the CSV line.
// The indexes in the CSV line start with 0.
c.Header(map[string]int{"Name": 0, "Balance": 2})
// Struct we wil lpopulate with data
p := &person2{}
// Set values from CSV line to person2 structure
err := c.SetData(p)
// Do work with p
// Set values from the second CSV line
err := c.SetData(p)
}
CustomBool() method allows you to set custom true / false values in CSV columns.
main () {
sr := csvutil.StringReadCloser("Y|N")
c := csvutil.NewCsvUtil(sr).CustomBool([]string{"Y"}, []string{"N"})
}
c := csvutil.NewCsvUtil(sr).Trim(" ") // Trim spaces from beginning and the end of volumn value
c := csvutil.NewCsvUtil(sr).Trim(" *") // Trim spaces and asterisks from beginning and the end of volumn value
p := &person{"Tom", 45, "aaa", 111.22, true}
csvLine, err := csvutil.ToCsv(p, "|", "YY", "NN")
fmt.Println(csvLine) // Prints: Tom|45|111.22|YY
...
p := &person2{}
err := c.SetData(p)
csvLine := c.LastCsvLine()
- Add writing CSV to file
Released under the MIT License. CSVUtil (c) Rafal Zajac rzajac@gmail.com