Go lang
Programs
read csv file
find variable type in golang
for loops in golang
MongoDB sort() Method
MongoDB Find
MongoDB FindOne
differences between arrays and slices
InsertOne MongoDB
Connecting to MongoDB
how to use range in golang
Modules
Create a Go module
Create app using Go Module
Install
Installing GO lang

Read CSV file in Go



Go provides encoding/csv package to read and write csv files

There are 2 ways to read the CSV file


  • csv.Reader.ReadAll() - Reads entire file at once, useful for small files, not recommended for large files
  • csv.Reader.Read() - will read the line by line, the best way to use for large files.

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


Read entire csv file at once (reading large files will consume memory).

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}]


read csv file line by line



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

how to use range in golang

posted on 2022-05-03 08:28:11 - Go lang Tutorials


find variable type in golang

find variable type in golang

posted on 2022-05-03 05:24:55 - Go lang Tutorials


for loops in golang

for loops in golang

posted on 2022-05-03 04:27:40 - Go lang Tutorials


Run MongoDB On WSL Ubuntu

Run MongoDB On WSL Ubuntu

posted on 2022-04-28 05:02:09 - mongodb Tutorials


Installing MongoDB

Installing MongoDB

posted on 2022-04-28 04:16:57 - mongodb Tutorials


MySQL TRIGGERS

MySQL - Triggers

posted on 2019-11-29 21:44:07 - mysql Tutorials