https://github.com/programmfabrik/go-csvx
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.
https://github.com/programmfabrik/go-csvx
csv csv-encoding go golang typed typed-csv
Last synced: 5 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/programmfabrik/go-csvx
- Owner: programmfabrik
- License: apache-2.0
- Created: 2021-07-05T12:15:19.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-07-12T10:02:23.000Z (over 4 years ago)
- Last Synced: 2023-07-27T22:29:25.141Z (over 2 years ago)
- Topics: csv, csv-encoding, go, golang, typed, typed-csv
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-csvx
[](https://github.com/programmfabrik/go-csvx/actions/workflows/tests.yml)
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:
```bash
go get github.com/programmfabrik/go-csvx
```
## Defining a csv
**Typed:**
```csv
foo,bar,counter,names
string,string,int,"string,array"
test1,test2,10,"hello,world,how,is,it,going"
```
**Untyped:**
```csv
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:**
```go
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:
```txt
untyped data:
[]map[string]interface {}{map[string]interface {}{"bar":"world", "counter":"10", "foo":"hello", "names":"hello,world,how,is,it,going"}}
```
**Typed example:**
```go
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:
```txt
typed data:
[]map[string]interface {}{map[string]interface {}{"bar":"world", "counter":10, "foo":"hello", "names":[]string{"hello", "world", "how", "is", "it", "going"}}}
```