csvtag

package module
v2.0.7 Latest Latest
Warning

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

Go to latest
Published: May 6, 2020 License: GPL-3.0 Imports: 9 Imported by: 14

README

go-csv-tag

Read csv file from go using tags

godoc for artonge/go-csv-tag

Go goreportcard for artonge/go-csv-tag

Sourcegraph for artonge/go-csv-tag PRs Welcome

Install

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

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 content
err  := csvtag.LoadFromPath(
	"file.csv",                                   // Path of the csv file
	&tab,                                         // A pointer to the create slice
	csvtag.CsvOptions{                            // Load your csv with optional options
		Separator: ';',                           // changes the values separator, default to ','
		Header: []string{"name", "ID", "number"}, // specify custom headers
})

You can also load the data from an io.Reader with:

csvtag.LoadFromPath(youReader, &tab)

Or from a string with:

csvtag.LoadFromString(yourString, &tab)

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 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.DumpToWriter(tab, yourIOWriter)

Or dump to a string with:

str, err := csvtag.DumpToString(tab)

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 DumpToFile

func DumpToFile(slice interface{}, path string, options ...CsvOptions) error

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

func DumpToString

func DumpToString(slice interface{}, options ...CsvOptions) (string, error)

DumpToString - writes a slice content into a string. @param slice: An object typically of the form []struct, where the struct is using csv tag. @param options (optional): options for the csv parsing. @return a string and an error if one occures.

func DumpToWriter

func DumpToWriter(slice interface{}, writer io.Writer, options ...CsvOptions) error

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

func LoadFromPath

func LoadFromPath(path string, destination interface{}, options ...CsvOptions) error

LoadFromPath - Load csv from a path and put it in a array of the destination's type using tags. Example:

tabOfMyStruct := []MyStruct{}
err  := Load(
			"my_csv_file.csv",
			&tabOfMyStruct,
			CsvOptions{
				Separator: ';',
				Header: []string{"header1", "header2", "header3"
			}
		})

@param path: the path of the csv file. @param destination: object where to store the result. @param options (optional): options for the csv parsing. @return an error if one occurs.

func LoadFromReader

func LoadFromReader(file io.Reader, destination interface{}, options ...CsvOptions) error

LoadFromReader - Load csv from an io.Reader and put it in a array of the destination's type using tags. Example:

tabOfMyStruct := []MyStruct{}
err  := Load(
			myIoReader,
			&tabOfMyStruct,
			CsvOptions{
				Separator: ';',
				Header: []string{"header1", "header2", "header3"
			}
		})

@param file: the io.Reader. @param destination: object where to store the result. @param options (optional): options for the csv parsing. @return an error if one occurs.

func LoadFromString

func LoadFromString(str string, destination interface{}, options ...CsvOptions) error

LoadFromString - Load csv from string and put it in a array of the destination's type using tags. Example:

tabOfMyStruct := []MyStruct{}
err  := Load(
			myString,
			&tabOfMyStruct,
			CsvOptions{
				Separator: ';',
				Header: []string{"header1", "header2", "header3"
			}
		})

@param str: the string. @param destination: object where to store the result. @param options (optional): options for the csv parsing. @return an error if one occurs.

Types

type CsvOptions

type CsvOptions struct {
	Separator rune
	Header    []string
}

CsvOptions - options when loading or dumping csv.

Jump to

Keyboard shortcuts

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