Comma-Separated Value (CSV) file, it’s likely that applications like Excel export spreadsheets into CSV file or a custom application program encode it data into CSV format for you. I assumed you getting in UTF-8 encoded.

Instead of hard coding the filenames into the source code, filename will pass into the progam as parameters (arguments)

Public Dataset

https://www.kaggle.com/datasets

Our example dataset (snip)

DATE_TIME,PLANT_ID,SOURCE_KEY,DC_POWER,AC_POWER,DAILY_YIELD,TOTAL_YIELD
15-05-2020 00:00,4135001,1BY6WEcLGh8j5v7,0,0,0,6259559
15-05-2020 00:00,4135001,1IF53ai7Xc0U56Y,0,0,0,6183645
15-05-2020 00:00,4135001,3PZuoBAID5Wc2HD,0,0,0,6987759

First row, the header names, and from the second row our dataset separated by a comma each field match the header names

package main
 
import (
    "encoding/csv"
    "fmt"
    "os"
)
 
func main() {
 
    file, err := os.Open(os.Args[1])
    if err != nil {
        fmt.Println(err)
    }
    reader := csv.NewReader(file)
    records, _ := reader.ReadAll()
    // Dump the list 
    fmt.Println(records)
}

Resources

https://golang.org/pkg/encoding/csv/

https://golang.org/pkg/encoding/csv/#pkg-examples

Categories: csv