There are 2 ways to read the CSV file
In below example, we use csvdata.csv file, and the content of the file is following.
1, Olivia, 23
2, Emma, 24
3, Charlotte, 22
In below example, we will open csv file, initialize the csv.Reader and read all data at once into a [][]string slice.
The csvReader.ReadAll(), returns [][]string slice object, which the first index is the line number and the second index contains comma-separated values.
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"strconv"
)
type Person struct {
Id int
Name string
Age int
}
func createPersonList( csvsliceddata [][]string) []Person {
// translate csv data into person list
var Persons []Person
// loop through all csv lines
for _, csvline := range csvsliceddata {
var objPerson Person
//loop through all csv fields
for fieldindex, fields := range csvline {
switch fieldindex {
case 0:
objPerson.Id, _ = strconv.Atoi( fields)
break
case 1:
objPerson.Name = fields
break
case 2:
objPerson.Age, _ = strconv.Atoi( fields)
break
}
}
Persons = append(Persons, objPerson)
}
return Persons
}
func main() {
//open csv file
objfile, err := os.Open("csvdata.csv")
//handle error, if file not found
if err != nil {
log.Fatal(err)
}
// read csv data using csv.ReadAll
csvReader := csv.NewReader( objfile)
csvdata, err := csvReader.ReadAll()
if err != nil {
log.Fatal(err)
}
// convert csv file content into person list
persons := createPersonList( csvdata)
// close the file, we are done with reading
objfile.Close()
fmt.Printf("%+v\n", persons)
}
Program Output:
[{Id:1 Name:Olivia Age:23} {Id:2 Name:Emma Age:24} {Id:3 Name:Charlotte Age:22}]
below is the example to read csv file line by line,
Read CSV file line by line using csv.Reader.Read() method. this csv.Reader.Read() method returns EOF if reached to end of file in error object.
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"io"
"strconv"
)
type Person struct {
Id int
Name string
Age int
}
func main() {
//open csv file
objfile, err := os.Open("csvdata.csv")
//handle error, if file not found
if err != nil {
log.Fatal(err)
}
//array to hold person data
var Persons []Person
// read csv data using csv.ReadAll
csvReader := csv.NewReader( objfile)
for {
fields, err := csvReader.Read()
//if reached end of file, break the loop
if err == io.EOF {
break
}
//if encounted with error exit
if err != nil {
log.Fatal(err)
}
//person object to hold the translated csv line
var objPerson Person
//loop through all csv fields
for fieldindex, field := range fields {
switch fieldindex {
case 0:
objPerson.Id, _ = strconv.Atoi( field)
break
case 1:
objPerson.Name = field
break
case 2:
objPerson.Age, _ = strconv.Atoi( field)
break
}
}
//append to array
Persons = append(Persons, objPerson)
}
// close the file, we are done with reading
objfile.Close()
fmt.Printf("%+v\n", Persons)
}
how to use range in golang
posted on 2022-05-03 08:28:11 - Go lang Tutorials
find variable type in golang
posted on 2022-05-03 05:24:55 - Go lang Tutorials