Skip to content
/ csv2 Public

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

License

Notifications You must be signed in to change notification settings

cinar/csv2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages