Important
The ReadCSV function returnes each row within the CSV file as an array
Example of a Go Program using the ReadCSV function:
package main
import (
"fmt"
"github.com/ellwould/csvcell"
"strings"
)
func main() {
dirPath := "/"
fileName := "example.csv"
// Reading a CSV file where each row has 3 columns
read := csvcell.ReadCSV(dirPath, fileName)
for _, read := range read {
column1 := strings.Join((read[0:][0:1]), ", ")
column2 := strings.Join((read[0:][1:2]), ", ")
column3 := strings.Join((read[0:][2:3]), ", ")
fmt.Println(column1, column2, column3)
}
}
Output in CLI:
Name Type Quantity
Apple Fruit 1
Pineapple Fruit 2
Pear Fruit 6
Orange Fruit 5
Grape Fruit 3
Carrot Vegetable 2
Lemon Fruit 8
Cherry Fruit 55
Banana Fruit 34
Broccoli Vegetable 9
The example.csv file used:
| Name | Type | Quantity |
|---|---|---|
| Apple | Fruit | 1 |
| Pineapple | Fruit | 2 |
| Pear | Fruit | 6 |
| Orange | Fruit | 5 |
| Grape | Fruit | 3 |
| Carrot | Vegetable | 2 |
| Lemon | Fruit | 8 |
| Cherry | Fruit | 55 |
| Banana | Fruit | 34 |
| Broccoli | Vegetable | 9 |
Important
Example of a Go Program using the WriteCSV function:
package main
import (
"github.com/ellwould/csvcell"
)
func main() {
dirPath := "/"
fileName := "example.csv"
// Data to write to CSV
produce := "Broccoli"
produceType := "Vegetable"
quantity := "9"
// Variable with all strings concatenated and commas included inbetween
data := produce + "," + produceType + "," + quantity
// WriteCSV function called from csvcell package
// (Directory Path, name of CSV file, commas to prepend, data to write to the file, commas to append)
csvcell.WriteCSV(dirPath, fileName, 0, data, 0)
}Note
For a list of abbreviations and there meanings used throughout this repository please refer to this README