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.
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
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"
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
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"}}}