The package provides a simple typed parser for csv/dsv files.
go get github.com/tomaspavlic/csv
import (
// import the package
"github.com/tomaspavlic/csv"
)
Only exportable fields are considered. The name of the field is used unless Tag csv
is used to specify actual name of a column in CSV.
// define struct type
type Taxable struct {
Index int
Description string `csv:"Item"`
Cost float32
Tax float32
Total float32
}
Parses each line into a item of given slice
func main() {
response, _ := http.Get("https://people.sc.fsu.edu/~jburkardt/data/csv/taxables.csv")
// define a slice with a type
var taxables []Taxable
// initiate the reader
r := csv.NewReader(response.Body)
// read all lines into the referenced slice
err := r.ReadAll(&taxables)
if err != nil {
fmt.Println(err)
}
fmt.Println(taxables)
}
TimeLayout - layout for parsing a time.Time, see format (default time.RFC3339)
Delimiter - change delimiter (default comma ',')
Int, Int8, Int16, Int32, Int64, Float32, Float64, time.Time, String