CSV (Comma Separated Value) is a popular file format to store and share data. The CSV files are widely used in web applications to export and import dynamic data.
In our previous tutorial you have learned how to parse JSON file in Golang. In this tutorial you will learn how to read and parse CSV file data in Golang. The Go provides encoding/csv
package to read CSV file. We will use this package to handle CSV file parsing.
We will cover this tutorial step by step to create a CSV file with some sample data. Then we will open CSV file and read CSV file data and store CSV file data into an object. Then display CSV file data using object.
Also, read:
Step1: Create CSV File
First we will create a CSV file emp.csv
using below data to read the read and parse the CSV file.
name,age,city Garry,30,Newyork Jhone,40,Paris Adam,50,London William,20,Sydney
Step2: Create Golang File
We will create main.go
file and import necessary packages. We will import encoding/csv
to read CSV file.
package main import ( "encoding/csv" "fmt" "os" )
Step3: Open CSV File
After creating CSV file emp.csv
, we will open the CSV using os
package from Go
. After opening the CSV file, we will handle CSV file parsing and close the file at the end of function.
csvFile, err := os.Open("emp.csv") if err != nil { fmt.Println(err) } fmt.Println("Successfully Opened CSV file") defer csvFile.Close()
Step4: Read CSV File Data with Golang
Now we will parse the employee CSV data file by defining structs. We will read CSV file into a variable using csv.NewReader
method. We will read all file data using ReadAll()
method. Then we will Loop through CSV lines and turn into object. Finally we will print CSV data line using object.
package main import ( "encoding/csv" "fmt" "os" ) type empData struct { Name string Age string City string } func main() { csvFile, err := os.Open("emp.csv") if err != nil { fmt.Println(err) } fmt.Println("Successfully Opened CSV file") defer csvFile.Close() csvLines, err := csv.NewReader(csvFile).ReadAll() if err != nil { fmt.Println(err) } for _, line := range csvLines { emp := empData{ Name: line[0], Age: line[1], City: line[2], } fmt.Println(emp.Name + " " + emp.Age + " " + emp.City) } }
Conclusion
Hopefully this tutorial helped you to handle CSV data file parsing to read CSV file data in Golang. If you find this tutorial helpful then please let us know in the comments section below.
You may also like:
- Regular Expressions in Golang
- Creating REST API with Golang
- How to Delete Files in Golang
- Working with Structs in Golang
- Working with Date and Time in Golang
- How to Make HTTP Requests in Golang
- Working with Channels in Golang
- Concurrency with Goroutines in Golang
- Working with Maps in Golang
- Marshalling and Unmarshalling in Golang
- AWS S3 File Upload in Golang
- Write Data to CSV File using Golang
- Parsing JSON Data using Golang
- Read File Line by Line using Golang
Thanks for the tutorial!
A detail…
“First we will create a CSV file emp.json using below…” should probably have been “First we will create a CSV file emp.csv using below…”
changes done, many thanks 🙂
Thanks for the tutorial. However, I am using this code and the first line of the csv data is being read in. Can you modify the code to not read in the csv header line, else it makes no sense. Thanks
I will update this ASAP, thanks!