https://github.com/cinar/csv2
Csv2 is a lightweight Golang module for reading CSV files as individual rows or as a table.
https://github.com/cinar/csv2
csv csv-parser golang golang-package
Last synced: 2 months ago
JSON representation
Csv2 is a lightweight Golang module for reading CSV files as individual rows or as a table.
- Host: GitHub
- URL: https://github.com/cinar/csv2
- Owner: cinar
- License: mit
- Created: 2021-06-08T23:11:18.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-10-10T19:27:56.000Z (over 4 years ago)
- Last Synced: 2025-10-24T21:39:42.863Z (5 months ago)
- Topics: csv, csv-parser, golang, golang-package
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
[](https://godoc.org/github.com/cinar/csv2)
[](https://opensource.org/licenses/MIT)
[](https://travis-ci.com/cinar/csv2)
# Csv2 Go
Csv2 is a lightweight Golang module for reading CSV files as individual rows or as a table.
## Usage
Install package.
```bash
go get github.com/cinar/csv2
```
Import Csv2.
```Golang
import (
"github.com/cinar/csv2"
)
```
### Reading as individual rows
Given that the CSV file contains the following columns.
```CSV
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.
```Golang
// 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.
```Golang
var prices []dailyPrice
```
Use the [ReadRowsFromFile](https://pkg.go.dev/github.com/cinar/csv2#ReadRowsFromFile) function to read the CSV file into the slice.
```Golang
err := csv2.ReadRowsFromFile(testFile, true, &prices)
if err != nil {
return err
}
```
### Reading as a table
Define a structure for the table.
```Golang
// 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.
```Golang
prices := stockPrices{}
```
Use the [ReadTableFromFile](https://pkg.go.dev/github.com/cinar/csv2#ReadRowsFromFile) function to read the CSV file into the table.
```Golang
err := csv2.ReadTableFromFile(testFile, true, &prices)
if err != nil {
t.Fatal(err)
}
```
## License
The source code is provided under MIT License.