csv2

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2021 License: MIT Imports: 10 Imported by: 0

README

GoDoc License Build Status

Csv2 Go

Csv2 is a lightweight Golang module for reading CSV files as individual rows or as a table.

Usage

Install package.

go get github.com/cinar/csv2

Import Csv2.

import (
    "github.com/cinar/csv2"
)
Reading as individual rows

Given that the CSV file contains the following columns.

date,close,high,low,open,volume,adjClose,adjHigh,adjLow,adjOpen,adjVolume,divCash,splitFactor
2015-09-18 00:00:00+00:00,43.48,43.99,43.33,43.5,63143684,39.5167038561,39.9802162518,39.3803766809,39.534880812800004,63143684,0.0,1.0

Define a structure for each individual row.

// Daily price structure for each row.
type dailyPrice struct {
	Date        time.Time `format:"2006-01-02 15:04:05-07:00"`
	Close       float64
	High        float64
	Low         float64
	Open        float64
	Volume      int64
	AdjClose    float64
	AdjHigh     float64
	AdjLow      float64
	AdjOpen     float64
	AdjVolume   int64
	DivCash     float64
	SplitFactor float64
}

Csv2 allows you to associate additional information about the colums through the tags. The following additional information is currently supported.

Tag Description Example
header Column header for the field. header:"Date"
format Date format for parsing. format:"2006-01-02 15:04:05-07:00"

Define an instance of a slice of row structure.

var prices []dailyPrice

Use the ReadRowsFromFile function to read the CSV file into the slice.

err := csv2.ReadRowsFromFile(testFile, true, &prices)
if err != nil {
    return err
}
Reading as a table

Define a structure for the table.

// Stock prices structure for all columns.
type stockPrices struct {
	Date        []time.Time `format:"2006-01-02 15:04:05-07:00"`
	Close       []float64
	High        []float64
	Low         []float64
	Open        []float64
	Volume      []int64
	AdjClose    []float64
	AdjHigh     []float64
	AdjLow      []float64
	AdjOpen     []float64
	AdjVolume   []int64
	DivCash     []float64
	SplitFactor []float64
}

Define an instance of the table structure.

prices := stockPrices{}

Use the ReadTableFromFile function to read the CSV file into the table.

err := csv2.ReadTableFromFile(testFile, true, &prices)
if err != nil {
    t.Fatal(err)
}

License

The source code is provided under MIT License.

Documentation

Index

Constants

View Source
const (
	// Header name
	TagHeader = "header"

	// Format name
	TagFormat = "format"
)

Variables

This section is empty.

Functions

func ReadRowsFromFile

func ReadRowsFromFile(fileName string, hasHeader bool, rows interface{}) error

Read rows from file.

func ReadRowsFromReader

func ReadRowsFromReader(reader io.Reader, hasHeader bool, rows interface{}) error

Read rows from reader.

func ReadTableFromFile

func ReadTableFromFile(fileName string, hasHeader bool, rows interface{}) error

Read table from file.

func ReadTableFromReader

func ReadTableFromReader(reader io.Reader, hasHeader bool, table interface{}) error

Read table from reader.

Types

This section is empty.

Jump to

Keyboard shortcuts

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