Read csv file from go using tags

go-csv-tag

Read csv file from Go using tags

godoc for artonge/go-csv-tag

Go goreportcard for artonge/go-csv-tag

PRs Welcome

The project is in maintenance mode.

It is kept compatible with changes in the Go ecosystem but no new features will be developed. PR could be accepted.

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.LoadFromReader(youReader, &tab)

Or from a string with:

csvtag.LoadFromString(yourString, &tab)

Dump

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
	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
Comments
  • Have you tested the LoadFromPath?

    Have you tested the LoadFromPath?

    First use the example DumpToFile to generate a csv test file, then use LoadFromPath to load this file, panic happened

    generated csv file : name, ID, number 'name1', 1, 1.2

    panic: runtime error: index out of range [1] with length 1

    goroutine 1 [running]: github.com/artonge/go-csv-tag.mapToDestination(0xc0000c7f48, 0x3, 0x3, 0xc0000b4 330, 0x2, 0x2, 0x4bbf00, 0xc000096560, 0x3, 0xc0000b4330) E:/Go/gopath/src/github.com/artonge/go-csv-tag/load.go:182 +0xe9f github.com/artonge/go-csv-tag.LoadFromReader(0x503ac0, 0xc0000ca028, 0x4bbf00, 0 xc000096560, 0xc0000c7f28, 0x1, 0x1, 0xc00008c8f0, 0xc0000a00c0) E:/Go/gopath/src/github.com/artonge/go-csv-tag/load.go:48 +0x17e github.com/artonge/go-csv-tag.LoadFromPath(0x4e52c4, 0x11, 0x4bbf00, 0xc00009656 0, 0xc0000c7f28, 0x1, 0x1, 0x0, 0x0) E:/Go/gopath/src/github.com/artonge/go-csv-tag/load.go:78 +0x10b

  • Fail to load content with trailing spaces

    Fail to load content with trailing spaces

    The library fails to parse CSV files that contain lines with trailing spaces. I came across this issue when trying to read a GTFS file using the go-gtfs library.

    E.g.:

    header1, header2, header3
    line1, 1, 1.2                                                                                                                                                                                                                                                                                                                         
    line2, 2, 2.3                                                                                                                                                                                                                                                                                                                         
    line3, 3, 3.4           
    

    Error produced:

    error mapping csv from path csv_files/trailingSpaces.csv:
            ==> error mapping the content to the destination
            ==> line: [line1 1 1.2                ] to slice: {line1 1 0}:
            ==> error parsing float '1.2                ':
            ==> strconv.ParseFloat: parsing "1.2                ": invalid syntax
    
  • My attempt at Dump function

    My attempt at Dump function

    Thanks again for writing this package! I am pretty new to Golang, but I really like this language. I have been trying to use it as my main language. Feel free to tell me any ideas you have to make this pull request better.

    Side note: I made the second param into Dump an io.Writer because it made the functionality easier to test and it did not limit the functionality. Let me know your thoughts on it!

  • Commnent errors

    Commnent errors

    I noticed that you are from Belgium, so what may seem strange to me may be normal to you. I am referring to treat vs treate and occure vs occur.

    In any case, thanks for creating this project! It looks good and I will probably start using it soon. Have a good day

  • Float32 loading problem

    Float32 loading problem

    Hello,

    Thanks for this awesome tool, it helps me a lot for many different projects.

    For the purpose of the PR, I have a struct with float32 fields and if I need to parse csv files into those structs. I realize it is not workings as expected , I am getting 0 value for those float32 fields. If I set those fields to float64 , it is being parsed as expected. I m a newbie to golang and I am not really sure this is intended or not. I have just created this PR , to fix this issue, please fell free to accept/change/reject for the matching cause.

    Thanks!

  • int64 support.

    int64 support.

    Hi I was trying to use this package to decode a struct with int64 fields and It wasn't working. There is a simple fix for this in load.go:

    func storeValue(rawValue string, valRv reflect.Value) error {
    	switch valRv.Kind() {
    	case reflect.String:
    		valRv.SetString(rawValue)
    	case reflect.Int64: // Add this.
    		fallthrough
    	case reflect.Int:
    		value, err := strconv.ParseInt(rawValue, 10, 64)
    		if err != nil && rawValue != "" {
    			return fmt.Errorf("error parsing int '%v':\n	==> %v", rawValue, err)
    
    		}
    
  • Add way to specify notation to be used for floats

    Add way to specify notation to be used for floats

    When Dumping structs containing floats, the default representation is used. For high exponents, this is scientific notation, e.g. 1e-06 for 0.000001.

    This is incompatible with some specifications that use csv. Currently, there's no way of specifying which representation go-csv-tag should use. Similar situations may arrise for other types, like if an integer should be duped as a hexadeximal value.

  • Dump

    Dump

    Thanks again for writing this package! I am pretty new to Golang, but I really like this language, so I have been trying to use it as my main language. Feel free to tell me any ideas you have to make this pull request better.

    Side note: I made the second param into Dump an io.Writer because it made the functionality easier to test and it did not limit the overall functionality of Dump. Let me know what you think!

  • bug fix: Can't get the last value if header end with white space.

    bug fix: Can't get the last value if header end with white space.

    hi, As you can see, if the cvs file's header end with white space, the lib can't load the last value correctly. i fix it with strings.TrimSpace() and the test case is provided.

  • Add option to configure CSV reader LazyQuote attribute

    Add option to configure CSV reader LazyQuote attribute

    This change allows to use go-csv-tag lib with CSV files having lazy quotes inside. Refactored load.go so that CSV reader creation is separated from usage, to simplify use of parameters.

  • GoDoc reference

    GoDoc reference

    I just noticed that the godoc reference was referencing the example you have in the source code. I capitalized the field names so that it matches the README

csv reader/writer and csv generator.

IO csv reader sample version 0.0.1-SNAPSHOT Goals: main: read huge file, hex substring, write to new file. repo has 2 version. v1 can read a file and

Nov 4, 2021
Read a tar file contents using go1.16 io/fs abstraction
Read a tar file contents using go1.16 io/fs abstraction

go-tarfs Read a tar file contents using go1.16 io/fs abstraction Usage ⚠️ go-tarfs needs go>=1.16 Install: go get github.com/nlepage/go-tarfs Use: pac

Dec 1, 2022
Simple go script that converts csv file into a json document

csv-go-parser Simple go script that converts csv file into a json document. CSV Input: id,first_name,last_name,email,avatar,ip_address 1,Pauline,Hirth

Jun 4, 2022
Extract profiles and tasks information from CSV file

Footsite-Bot ideas from jw6602 Extract profiles and tasks information from CSV f

Nov 25, 2022
gsheet is a CLI tool (and Golang package) for piping csv data to and from Google Sheets

gsheet Table of Contents Introduction Why? Installation Authentication and Authorization What about OAuth authentication? CLI Usage Sheet commands Dri

Nov 15, 2022
Allows parsing CSV files into custom structs and implements required fields that can't be empty

Welcome to Go Custom CSV Parser ?? Allows parsing CSV files into custom structs and implements required fields that can't be empty ?? Homepage Install

Nov 9, 2021
A simple utility for validating CSV columns

gompare A simple utility for validating CSV columns Building In project directly, run go build Usage ./gompare --template-file=template.csv --input-fi

Feb 3, 2022
GoCsv is a library written in pure Go to use csv data more comfortable

GoCsv GoCsv is a library written in pure Go to use csv data more comfortable Supported Go version golang >= 1.13 Installation go get github.com/shr004

Nov 1, 2022
This program let you create a DataSet (.CSV) with all TedTalks

TedTalks-Scraper This program let you create a file .CSV with all information from TedTalks, including: Title Description Views (Number of Views) Auth

Dec 26, 2021
CSV asynchronous I/O package for go.

golib/csvio selectでの非同期処理に対応した、csvベースでの読み込み/書き込みを行うライブラリです。 csvio.Config CSVのコンマの値や、クォートの有無など、扱うCSVに対するフォーマットを指定できます。 csvio.Reader selectでの非同期処理に対応した、

Jan 21, 2022
FujiSimuRecipesGen - A Golang program to generate Fujifilm simulations from recipes of such simulations in CSV

FujiSimuRecipesGen - A Golang program to generate Fujifilm simulations from recipes of such simulations in CSV

Aug 12, 2022
Read data from rss, convert in pdf and send to kindle. Amazon automatically convert them in azw3.

Kindle-RSS-PDF-AZW3 The Kindle RSS PDF AZW3 is a personal project. The Kindle RSS PDF AZW3 is a personal project. I received a Kindle for Christmas, a

Jan 10, 2022
go-fastdfs 是一个简单的分布式文件系统(私有云存储),具有无中心、高性能,高可靠,免维护等优点,支持断点续传,分块上传,小文件合并,自动同步,自动修复。Go-fastdfs is a simple distributed file system (private cloud storage), with no center, high performance, high reliability, maintenance free and other advantages, support breakpoint continuation, block upload, small file merge, automatic synchronization, automatic repair.(similar fastdfs).
go-fastdfs 是一个简单的分布式文件系统(私有云存储),具有无中心、高性能,高可靠,免维护等优点,支持断点续传,分块上传,小文件合并,自动同步,自动修复。Go-fastdfs is a simple distributed file system (private cloud storage), with no center, high performance, high reliability, maintenance free and other advantages, support breakpoint continuation, block upload, small file merge, automatic synchronization, automatic repair.(similar fastdfs).

中文 English 愿景:为用户提供最简单、可靠、高效的分布式文件系统。 go-fastdfs是一个基于http协议的分布式文件系统,它基于大道至简的设计理念,一切从简设计,使得它的运维及扩展变得更加简单,它具有高性能、高可靠、无中心、免维护等优点。 大家担心的是这么简单的文件系统,靠不靠谱,可不

Jan 8, 2023
File Processor in Concurrency Pattern using Golang goroutine.
 File Processor in Concurrency Pattern using Golang goroutine.

File Processor in Concurrency Pattern Implement a file processor solution in concurrency pattern using Golang goroutine. Get Started Run docker-compos

Sep 16, 2022
Abstract File Storage

afs - abstract file storage Please refer to CHANGELOG.md if you encounter breaking changes. Motivation Introduction Usage Matchers Content modifiers S

Dec 30, 2022
a tool for handling file uploads simple

baraka a tool for handling file uploads for http servers makes it easier to make operations with files from the http request. Contents Install Simple

Nov 30, 2022
Bigfile -- a file transfer system that supports http, rpc and ftp protocol https://bigfile.site
Bigfile -- a file transfer system that supports http, rpc and ftp protocol   https://bigfile.site

Bigfile ———— a file transfer system that supports http, rpc and ftp protocol 简体中文 ∙ English Bigfile is a file transfer system, supports http, ftp and

Dec 31, 2022
Go file operations library chasing GNU APIs.
Go file operations library chasing GNU APIs.

flop flop aims to make copying files easier in Go, and is modeled after GNU cp. Most administrators and engineers interact with GNU utilities every da

Nov 10, 2022
File system event notification library on steroids.

notify Filesystem event notification library on steroids. (under active development) Documentation godoc.org/github.com/rjeczalik/notify Installation

Dec 31, 2022