gncsv

package
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: MIT Imports: 14 Imported by: 2

README

GNcsv

This library provides reading and writing to CSV, TSV and PSV (pipe-separated) files.

Usage

See examples in gncsv_test.go file.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToRow added in v0.5.1

func ToRow(fields []string) string

ToRow converts a slice of strings representing a CSV row into a single string with comma separation.

Types

type GnCSV

type GnCSV interface {
	Reader
	Writer
}

GnCSV combines Reader and Writer interfaces, providing a unified interface for reading and writing CSV data.

func New

func New(cfg config.Config) GnCSV

New creates a new CSV or TSV/PSV reader/writer based on the provided configuration. If the ColSep in the config is a comma, it creates a CSV reader/writer. Otherwise, it creates a TSV reader/writer.

func NewCSV

func NewCSV(cfg config.Config) GnCSV

New creates a new CSV or TSV/PSV reader/writer based on the provided configuration. If the ColSep in the config is a comma, it creates a CSV reader/writer. Otherwise, it creates a TSV reader/writer.

func NewTSV

func NewTSV(cfg config.Config) GnCSV

NewTSV creates a new GnCSV instance.

type Reader

type Reader interface {
	// ReadSlice reads a portion of the CSV data starting at the specified offset
	// and up to the given limit. Each row is represented as a slice of strings.
	//
	// Parameters:
	//   - offset: The starting position of the rows to read.
	//   - limit: The maximum number of rows to read.
	//
	// Returns:
	//   - [][]string: A slice of string slices, where each inner slice
	//     represents a row in the CSV.
	//   - error: An error if any issue occurs during reading; otherwise, nil.
	ReadSlice(offset, limit int) ([][]string, error)

	// Read reads all rows from the CSV data and sends each row as a slice of
	// strings to the provided channel. The function processes the data until the
	// end of the file or an error occurs.
	//
	// Parameters:
	//   - ctx: A context to control the cancellation or timeout of the read
	//     operation.
	//   - chOut: A channel to which the function sends each row of the CSV as a
	//     slice of strings.
	//
	// Returns:
	//   - int: The total number of rows read.
	//   - error: An error if any issue occurs during reading; otherwise, nil.
	Read(context.Context, chan<- []string) (int, error)

	// ReadChunks reads data in chunks of the specified size and sends each chunk
	// to the provided channel chOut. The function returns the total number of
	// records read and an error, if any occurs during the process.
	//
	// Parameters:
	//   - ctx: A context to control the cancellation or timeout of the read
	//     operation.
	//   - chOut: A channel to which the function sends slices of string slices,
	//     representing chunks of data.
	//   - chunkSize: The size of each chunk to be read.
	//
	// Returns:
	//   - int: The total number of records read.
	//   - error: An error if any issue occurs during reading; otherwise, nil.
	ReadChunks(
		ctx context.Context,
		chOut chan<- [][]string,
		chunkSize int,
	) (int, error)

	// Headers returns the headers of the CSV file as a slice of strings.
	Headers() []string

	// F is a field accessor. If the field with the given name exists, it returns
	// the value of the field in the row. If not, returns empty string and an
	// error.
	F(row []string, f string) string
}

Reader is an interface that defines methods for reading and processing CSV data. It provides functionality to read data in slices, stream rows through a channel, and retrieve headers.

type Writer

type Writer interface {
	// WriteStream writes CSV data received from the provided channel. Each
	// string slice received from the channel represents a row in the CSV.
	// It uses a context for cancellation.
	WriteStream(context.Context, <-chan []string) error
}

Writer defines an interface for writing CSV data.

Example
package main

import (
	"bytes"
	"context"
	"fmt"

	"github.com/gnames/gnfmt/gncsv"
	"github.com/gnames/gnfmt/gncsv/config"
)

func main() {
	var b bytes.Buffer
	writeOpts := []config.Option{
		config.OptWriter(&b),
		config.OptHeaders([]string{"header1", "header2", "header3"}),
	}
	cfg, err := config.New(writeOpts...)
	if err != nil {
		panic(err)
	}
	w := gncsv.New(cfg)

	ch := make(chan []string)
	go func() {
		defer close(ch)
		for range 3 {
			ch <- []string{"val1", "val2", "val3"}
		}
	}()

	if err := w.WriteStream(context.Background(), ch); err != nil {
		panic(err)
	}

	fmt.Println(b.String())
}
Output:

header1,header2,header3
val1,val2,val3
val1,val2,val3
val1,val2,val3

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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