go_csv

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2024 License: MIT Imports: 5 Imported by: 0

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.

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
}

Documentation

Index

Constants

View Source
const WindowSize = 5

Variables

This section is empty.

Functions

func Generate

func Generate(ctx context.Context, filePath string, headers []string, data [][]string) error

Generate function to generate the csv file

Types

type GoCsv

type GoCsv struct {
	Input        chan []string
	Output       chan []string
	File         *os.File
	WindowValues [int64(WindowSize)][]string
	Position     int
	Ctx          context.Context
}

func NewGoCsv

func NewGoCsv(ctx context.Context, input, output chan []string, file *os.File) *GoCsv

func (*GoCsv) AddData

func (gc *GoCsv) AddData() error

func (*GoCsv) AddHeader

func (gc *GoCsv) AddHeader(headers []string) error

func (*GoCsv) Cancel

func (gc *GoCsv) Cancel()

func (*GoCsv) GenerateRowBatch

func (gc *GoCsv) GenerateRowBatch() []string

func (*GoCsv) InputData

func (gc *GoCsv) InputData(data [][]string)

func (*GoCsv) Worker

func (gc *GoCsv) Worker()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL