csv

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2022 License: MIT Imports: 5 Imported by: 0

README

CSV

Go Reference

A golang package that extends the standard library's csv.Reader with ForEach to make stream processing large files a breeze without loading the entire file in memory. Furthermore, this package seamlessly adds gzip support for even easier processing at scale.

Package Usage

import (
    "github.com/prophittcorey/csv"
    "log"
)

if reader, err := csv.NewReaderFromFile("testfiles/people.csv.gz"); err == nil {
  /* give the reader a head's up... no pun intended, kinda */
  reader.Header = true

  rows, err := reader.ForEach(func(row *Row) error {
    if firstName, ok := row.Get("first_name"); ok {
      log.Printf("Hello, %s!\n", firstName)
    }

    /* stream processing halts if non-nil is returned */
    return nil
  })

  log.Printf("We processed %d rows; errors? %v\n", rows, err == nil)
}

License

The source code for this repository is licensed under the MIT license, which you can find in the LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	*csv.Reader

	Header bool /* if true, will extract the header row first */
}

func NewReader

func NewReader(r io.Reader) (*Reader, error)

func NewReaderFromFile

func NewReaderFromFile(file string) (*Reader, error)

func (*Reader) ForEach

func (r *Reader) ForEach(cb func(*Row) error) (int, error)

type Row

type Row struct {
	// Headers stores a slice of the header columns (if Reader's Header is true).
	Headers []string

	// Data is the actual row data.
	Data []string
	// contains filtered or unexported fields
}

func (Row) Get

func (r Row) Get(header string) (string, bool)

Jump to

Keyboard shortcuts

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