Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/smacker/structcsv
Simple deserialization of csv file to slice of structs with struct tags
https://github.com/smacker/structcsv
csv deserialization golang
Last synced: 26 days ago
JSON representation
Simple deserialization of csv file to slice of structs with struct tags
- Host: GitHub
- URL: https://github.com/smacker/structcsv
- Owner: smacker
- Created: 2017-09-19T07:03:35.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-10-24T07:52:48.000Z (about 7 years ago)
- Last Synced: 2024-10-30T18:48:35.463Z (2 months ago)
- Topics: csv, deserialization, golang
- Language: Go
- Homepage: https://godoc.org/github.com/smacker/structcsv
- Size: 6.84 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CSV reader to structs
[![GoDoc](https://godoc.org/github.com/smacker/structcsv?status.png)](https://godoc.org/github.com/smacker/structcsv)
Very simple deserialization of csv file to slice of structs using struct tags.
If you also need serialization, take a look at [GoCSV](https://github.com/gocarina/gocsv).
## Installation
```go get -u github.com/smacker/structcsv```
## Example
```go
package mainimport (
"bytes"
"encoding/csv"
"fmt""github.com/smacker/structcsv"
)var csvContent = `client_id,client_name,age
1,Jose,28
2,Daniel,10
3,Vincent,54`type Client struct {
ID int `csv:"client_id"`
Name string `csv:"client_name"`
Age int `csv:"age"`
}func main() {
in := bytes.NewBufferString(csvContent)
r := structcsv.NewStructReader(csv.NewReader(in))headers, err := r.Headers()
if err != nil {
panic(err)
}
fmt.Println(headers)var clients []Client
if err := r.ReadAll(&clients); err != nil {
panic(err)
}
for _, c := range clients {
fmt.Printf("%+v\n", c)
}
}```
Output:
```
$ go run showcase.go
[client_id client_name age]
{ID:1 Name:Jose Age:28}
{ID:2 Name:Daniel Age:10}
{ID:3 Name:Vincent Age:54}
```