An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# go-csvx

[![unit-tests](https://github.com/programmfabrik/go-csvx/actions/workflows/tests.yml/badge.svg)](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"}}}
```