Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/michaelwp/go-csv

The go-csv package provides an easy-to-use interface for generating CSV files with concurrent processing
https://github.com/michaelwp/go-csv

concurrency csv go golang stream

Last synced: 6 days ago
JSON representation

The go-csv package provides an easy-to-use interface for generating CSV files with concurrent processing

Awesome Lists containing this project

README

        

# go-csv

The `go-csv` package provides an easy-to-use interface for generating CSV files with concurrent processing. This package is particularly useful for handling large datasets and performing stream processing.
For detail explanation about concurrent & streaming processing please visit [Stream and Concurrent Processing in Programming](https://goblog.dev/articles/39)

## Installation

To install the package, use `go get`:

```bash
go get github.com/michaelwp/go-csv
```

## Usage

### Generate Function

The `Generate` function creates a CSV file with the given headers and data.

#### Example:

```go
package main

import (
"context"
"fmt"
"log"
"strconv"
"time"

gocsv "github.com/michaelwp/go-csv"
)

func main() {
filePath := "output"
headers := []string{"No", "Name", "Age", "Occupation"}
data := GenerateSampleData(1000000)

ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

err := gocsv.Generate(ctx, filePath, headers, data)
if err != nil {
log.Println("failed to create csv", err)
return
}

fmt.Println("csv generated successfully")
}

func GenerateSampleData(rowNumber int) [][]string {
var data = make([][]string, rowNumber)

for i := 0; i < rowNumber; i++ {
noStr := strconv.Itoa(i + 1)
data[i] = []string{noStr, "John Doe", "30", "Engineer"}
}

return data
}