Parsing comma-separated data
There are multiple table data formats. CSV (comma-separated values) is one of the most basic formats largely used for data transport and export. There is no standard that defines CSV, but the format itself is described in RFC 4180.
This recipe introduces how to parse CSV-formatted data comfortably.
How to do it...
- Open the console and create the folder
chapter02/recipe10. - Navigate to the directory.
- Create a file named
data.csvwith the following content:
"Name","Surname","Age"
# this is comment in data
"John","Mnemonic",20
Maria,Tone,21- Create the
data.gofile with the following content:
package main
import (
"encoding/csv"
"fmt"
"os"
)
func main() {
file, err := os.Open("data.csv")
if err != nil {
panic(err)
}
defer file.Close()
reader := csv.NewReader(file)
reader.FieldsPerRecord = 3
reader...