Read and Write CSV file in go language!
Introduction
CSV (Comma Separated Value) files are a common way of storing data that can be read by various software applications. CSV files are plain text files that contain a list of data records separated by commas. Go provides an easy and efficient way to read and write CSV files.
In this article, we will learn how to read and write CSV files in Go.
Prerequisites
Before we start, we need to have Go installed on our system. We can download and install the latest version of Go from the official Go website.
Reading CSV Files
Go provides a built-in package called "encoding/csv" that allows us to read CSV files. We can use this package to read a CSV file and store its contents in a 2D slice.
Here is an example of how to read a CSV file:
package main
import (
"encoding/csv"
"fmt"
"os"
)
func main() {
// Open the CSV file
file, err := os.Open("data.csv")
if err != nil {
fmt.Println("Error:", err)
return
}
defer file.Close()
// Create a new reader
reader := csv.NewReader(file)
// Read all the records
records, err := reader.ReadAll()
if err != nil {
fmt.Println("Error:", err)
return
}
// Print the records
fmt.Println(records)
}
In this example, we first open the CSV file using the os.Open
()
function. If there is an error, we print it and return. Next, we create a new csv.Reader
and pass the file
object to it. Finally, we call the ReadAll()
function to read all the records from the CSV file and store them in a 2D slice called records
.
Writing CSV Files
To write data to a CSV file, we can use the same encoding/csv
package. We can create a new csv.Writer
object and write data to it using the Write()
method. We can also use the Flush()
method to flush the data to the file.
Here is an example of how to write data to a CSV file:
package main
import (
"encoding/csv"
"fmt"
"os"
)
func main() {
// Create a new file
file, err := os.Create("data.csv")
if err != nil {
fmt.Println("Error:", err)
return
}
defer file.Close()
// Create a new writer
writer := csv.NewWriter(file)
// Write some records
records := [][]string{
{"John", "Doe", "25"},
{"Jane", "Doe", "30"},
{"Tom", "Smith", "35"},
}
for _, record := range records {
err := writer.Write(record)
if err != nil {
fmt.Println("Error:", err)
return
}
}
// Flush the data to the file
writer.Flush()
}
In this example, we first create a new file using the os.Create()
function. If there is an error, we print it and return. Next, we create a new csv.Writer
and pass the file
object to it. We then write some records to the writer using the Write()
method. Finally, we call the Flush()
method to flush the data to the file.
Conclusion
In this article, we learned how to read and write CSV files in Go. Go provides a built-in package called encoding/csv
that makes it easy to read and write CSV files. We can use the csv.Reader
object to read CSV data.
I hope this helps, you!!
More such articles:
https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA
\==========================**=========================
If this article adds any value to you then please clap and comment.
Letβs connect on Stackoverflow, LinkedIn, & Twitter.