csv_parse

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 23, 2023 License: MIT Imports: 7 Imported by: 0

README

csv_parse Package

Inspired by gocsv, this package helps simplify parsing CSVs into go structs.

Usage

First, provide a CSV. For example, assume the file invoice.csv looks like this:

item_name,price_in_usd,qty
cookies,12.34,20
brownies,10.22,4

To use, first declare a struct that defines your CSV data model (see InvoiceRow below). Then, you can use the csv_parse library like so:

package main

import (
	"fmt"
	"os"

	"github.com/samc1213/gtfs-analyze/csv_parse"
)

type InvoiceRow struct {
	Item     string  `csv_parse:"item_name"`
	Price    float32 `csv_parse:"price_in_usd"`
	Quantity int32   `csv_parse:"qty"`
}

func main() {
	file, err := os.Open("invoice.csv")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	recordProvider, err := csv_parse.BeginParseCsv[InvoiceRow](file)
	if err != nil {
		panic(err)
	}

	newRecord, err := recordProvider.FetchNext()
	if err != nil {
		panic(err)
	}

	// Prints "cookies"
	fmt.Println(newRecord.Item)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EOF = errors.New("EOF")

Functions

func GetDecodeInfo

func GetDecodeInfo(t reflect.Type) (decodeInfo, error)

GetDecodeInfo reads the reflection tags

Types

type RecordProvider

type RecordProvider[T any] struct {
	// contains filtered or unexported fields
}

func BeginParseCsv

func BeginParseCsv[T any](input io.Reader) (*RecordProvider[T], error)

func (*RecordProvider[T]) FetchNext

func (r *RecordProvider[T]) FetchNext() (T, error)

type TypeFromCsvConverter

type TypeFromCsvConverter interface {
	ConvertFromCsv(string) error
}

Jump to

Keyboard shortcuts

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