Simple library that parses a CSV io.Reader and maps it to a given array struct.
- Works only with a struct that contains string, int, uint, bool, time and float fields.
go get -u github.com/licasterian/csvdecoder
Define your struct:
type YourStruct struct{
Field1 string
Field2 int
Field3 bool
Field4 float64
Field5 time.Time `csvDate:"2006-05-07"`
}
If you don't add 'csv' tags close to each struct's field, the lib will set the first field using the first column of csv's row, and so on. So the previous struct is the same as:
type Entry struct{
Field1 string `csv:"field1"`
Field2 int `csv:"field2"`
Field3 bool `csv:"field3"`
Field4 float64 `csv:"field4"`
Field5 time.Time `csv:"field5" csvDate:"2006-01-02"`
}
It's required to specify a csvDate
tag that will be used for parsing, following the rules describere here
decoder := csv.NewDecoder(reader)
var entries []*Entry
err := decoder.Decode(&entries)
if err != nil {
fmt.Printf("decoder.Decode error: %s\n", err.Error())
return
}
for _, entry := range entries {
fmt.Printf("%+v\n", *entry)
}