Skip to content

Simple deserialization of csv file to slice of structs with struct tags

Notifications You must be signed in to change notification settings

smacker/structcsv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CSV reader to structs

GoDoc

Very simple deserialization of csv file to slice of structs using struct tags.

If you also need serialization, take a look at GoCSV.

Installation

go get -u github.com/smacker/structcsv

Example

package main

import (
	"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}

About

Simple deserialization of csv file to slice of structs with struct tags

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages