Skip to content

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

License

Notifications You must be signed in to change notification settings

michaelwp/go-csv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Installation

To install the package, use go get:

go get github.com/michaelwp/go-csv

Usage

Generate Function

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

Example:

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
}

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages