https://github.com/aodin/csv2
Parse CSV files directly into Go structs
https://github.com/aodin/csv2
Last synced: 3 months ago
JSON representation
Parse CSV files directly into Go structs
- Host: GitHub
- URL: https://github.com/aodin/csv2
- Owner: aodin
- License: mit
- Created: 2014-04-04T16:59:14.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2017-03-14T19:58:27.000Z (almost 9 years ago)
- Last Synced: 2025-01-21T08:29:39.362Z (12 months ago)
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
csv2
====
Parse `csv` files directly into `struct` instances.
[](https://travis-ci.org/aodin/csv2)
Quickstart
----------
Parse the `csv` file:
ID,NAME,ABBREV
1,"United States",US
2,"Canada",CA
```go
package main
import (
"fmt"
"os"
"github.com/aodin/csv2" // Will import as csv!
)
type Country struct {
ID int64
Name string
Abbrev string
}
func (c Country) String() string {
return fmt.Sprintf("%d: %s", c.ID, c.Name)
}
func main() {
f, err := os.Open("./countries.csv")
if err != nil {
panic(err)
}
csvf := csv.NewReader(f)
// Discard the header
if _, err = csvf.Read(); err != nil {
panic(err)
}
var countries []Country
if err = csvf.Unmarshal(&countries); err != nil {
panic(err)
}
fmt.Println(countries)
}
```
Destination Types
-----------------
Supported field types for destination structs include:
* `string`
* `int64`
* `float64`
* `bool`
* `time.Time`
By default, `time.Time` fields will be parsed with the `RFC3339` layout. Alternative layouts can be specified by adding a `csv` tag to the field:
```go
type holiday struct {
Name string
Day time.Time `csv:"Jan _2"`
}
```
When a `csv` column is empty, the types above will error. You will want to use a pointer type, which will be set to `nil` when there is no content:
* `*int64`
* `*float64`
* `*bool`
* `*time.Time`
Writer
------
The package can also write slices of structs to an output, including a header row derived from struct field names!
```go
f, err := os.OpenFile("out.csv", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
panic(err)
}
defer f.Close()
writer := csv.NewWriter(f)
writer.WriteHeader(&countries)
writer.Marshal(&countries)
```
ID,Name,Abbrev
1,United States,US
2,Canada,CA
Happy Hacking!
aodin, 2014