Skip to content

go-csvx implements the csv encoder and extends it with various functionalities. This library supports a typed csv format. The second column defines the data type.

License

Notifications You must be signed in to change notification settings

programmfabrik/go-csvx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-csvx

unit-tests

go-csvx implements the csv encoder and extends it with various functionalities. This library supports a typed csv format. The second column defines the data type.

Getting started

Since this repository serves as a library for typed and untyped csv parsing, you need to define it as a Go dependency to work with it. To declare it as a dependency in your project, use the go get command from below to attach it to your project:

go get github.com/programmfabrik/go-csvx

Defining a csv

Typed:

foo,bar,counter,names
string,string,int,"string,array"
test1,test2,10,"hello,world,how,is,it,going"

Untyped:

foo,bar,counter,names
hello,world,10,"hello,world,how,is,it,going"

Supported data types

This library supports the following list of data types:

  • string
  • int64
  • int
  • float64
  • bool
  • "string,array"
  • "int64,array"
  • "float64,array"
  • "bool,array"
  • json
  • *string
  • *int64
  • *int
  • *float64
  • *bool
  • *"string,array"
  • *"int64,array"
  • *"float64,array"
  • *"bool,array"
  • *json

Examples

Untyped example:

package main

import (
    "fmt"

    "github.com/programmfabrik/go-csvx"
)

func main() {
    csv := csvx.CSVParser{
        Comma:            ',',
        Comment:          '#',
        TrimLeadingSpace: true,
        SkipEmptyColumns: true,
    }

    data, _ := csv.Untyped([]byte(
        `foo,bar,counter,names
        hello,world,10,"hello,world,how,is,it,going"`))

    fmt.Printf("untyped data:\n\t%+#v\n", data)
}

Result:

untyped data:
        []map[string]interface {}{map[string]interface {}{"bar":"world", "counter":"10", "foo":"hello", "names":"hello,world,how,is,it,going"}}

Typed example:

package main

import (
    "fmt"

    "github.com/programmfabrik/go-csvx"
)

func main() {
    csv := csvx.CSVParser{
        Comma:            ',',
        Comment:          '#',
        TrimLeadingSpace: true,
        SkipEmptyColumns: true,
    }

    data, _ := csv.Typed([]byte(
        `foo,bar,counter,names
        string,string,int,"string,array"
        hello,world,10,"hello,world,how,is,it,going"`))

    fmt.Printf("untyped data:\n\t%+#v\n", data)
}

Result:

typed data:
        []map[string]interface {}{map[string]interface {}{"bar":"world", "counter":10, "foo":"hello", "names":[]string{"hello", "world", "how", "is", "it", "going"}}}

About

go-csvx implements the csv encoder and extends it with various functionalities. This library supports a typed csv format. The second column defines the data type.

Topics

Resources

License

Stars

Watchers

Forks

Languages