csvtag

package module
Version: v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2019 License: GPL-3.0 Imports: 8 Imported by: 1

README

go-csv-tag

Read csv file from go using tags

godoc for artonge/go-csv-tag

Build Status cover.run go goreportcard for artonge/go-csv-tag

Sourcegraph for artonge/go-csv-tag PRs Welcome

Install

go get github.com/artonge/go-csv-tag

Example

Load

The csv file:

name, ID, number
name1, 1, 1.2
name2, 2, 2.3
name3, 3, 3.4

Your go code:

type Demo struct {                         // A structure with tags
	Name string  `csv:"name"`
	ID   int     `csv:"ID"`
	Num  float64 `csv:"number"`
}

tab := []Demo{}                             // Create the slice where to put the file content
err  := csvtag.Load(csvtag.Config{          // Load your csv with the appropriate configuration
  Path: "file.csv",                         // Path of the csv file
  Dest: &tab,                               // A pointer to the create slice
  Separator: ';',                           // Optional - if your csv use something else than ',' to separate values
  Header: []string{"name", "ID", "number"}, // Optional - if your csv does not contains a header
})

Dump

You go code:

type Demo struct {                         // A structure with tags
	Name string  `csv:"name"`
	ID   int     `csv:"ID"`
	Num  float64 `csv:"number"`
}

tab := []Demo{                             // Create the slice where to put the file content
	Demo{
		Name: "some name",
		ID: 1,
		Num: 42.5,
	},
}

err := csvtag.DumpToFile(tab, "csv_file_name.csv")

You can also dump the data into an io.Writer with

err := csvtag.Dump(tab, yourIOWriter)

The csv file written:

name,ID,number
some name,1,42.5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dump

func Dump(slice interface{}, writer io.Writer) error

Dump - writes a slice content into an io.Writer @param slice: an object typically of the form []struct, where the struct using csv tags @param writer: the location of where you will write the slice content to. Example: File, Stdout, etc @return an error if one occures

func DumpToFile

func DumpToFile(slice interface{}, filePath string) error

DumpToFile - writes a slice content into a file specified by filePath @param slice: An object typically of the form []struct, where the struct using csv tag @param filePath: The file path string of where you want the file to be created @return an error if one occures

func Load

func Load(config Config) error

Load - Load a csv file and put it in a array of the dest type This uses tags Example:

tabT := []Test{}
err  := Load(Config{
		Path: "csv_files/valid.csv",
		Dest: &tabT,
		Separator: ';',
		Header: []string{"header1", "header2", "header3"}
	})

The 'separator' and 'header' properties of the config object are optionals @param dest: object where to store the result @return an error if one occurs

Types

type Config

type Config struct {
	Path      string
	Dest      interface{}
	Separator rune
	Header    []string
}

Config struct to pass to the Load function

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL