https://github.com/datainq/csvlib
A simple CSV parsing library for Go.
https://github.com/datainq/csvlib
csv csv-parser golang golang-library
Last synced: 10 months ago
JSON representation
A simple CSV parsing library for Go.
- Host: GitHub
- URL: https://github.com/datainq/csvlib
- Owner: datainq
- License: apache-2.0
- Created: 2018-06-20T19:01:03.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-22T11:44:47.000Z (almost 8 years ago)
- Last Synced: 2025-03-01T14:17:35.831Z (about 1 year ago)
- Topics: csv, csv-parser, golang, golang-library
- Language: Go
- Size: 10.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# csvlib
A simple CSV parsing library for Go.
## How to use
One can define a parser:
```go
var parser = &csvlib.RowParser{
[]csvlib.Parser{
csvlib.Int64Parser{"ID"},
csvlib.Int32Parser{Name: "Number"}, // CKK
csvlib.TimeParser{"Timestamp", "2006-01-02 15:04:05"},
csvlib.StringParser{"Name"},
},
}
type Data struct {
ID int64
Number int32
Time time.Time
Name string
}
// and use it to parse row:
reader := csv.NewReader(r)
record, err := reader.Read() // reads one line
values, err := parser.Parse(record) // converts values
dat := Data{
values[0].Int64(),
values[1].Int32(),
values[2].Time(),
values[3].String(),
}
```
The most common usecase is when a you want to give the user
the possibility to choose columns. It can be configured though
a config file or UI.
Also it's pretty easy to create your own type of data
(e.g. Decimal with fixed precision), you must implement `Parser`
interface.
If you want to parse directly to structure, you may be interested in:
[github.com/gocarina/gocsv](https://github.com/gocarina/gocsv)
which is using a reflection mechanism and custom structure tag.