A Simple Guide to Working with CSV Files in Golang
Introduction
Working with CSV (Comma Separated Values) files is a common task in programming, particularly when working with large datasets. CSV files are lightweight, and easy to work with and create, making them a popular choice for data exchange and storage. In this article, we’ll look at how to work with CSV files in Golang.
Golang comes with a built-in package for working with CSV files, encoding/csv
. This package provides a Reader
and Writer
type that are used to read and write CSV files.
Before we get started, let’s create a simple CSV file to work with. Create a file called data.csv
and add the following data:
Reading CSV Files
There are two ways to read a CSV file. The first is to read the entire file at once, or line by line. We’ll look at both methods.
Read All
To read a CSV file, we’ll use the Reader
type from the encoding/csv
package. The Reader
type has a ReadAll
method that reads the entire CSV file and returns a nested slice of strings. Let’s look at an example:
The FieldsPerRecord
field on the Reader
type is used to specify the number of fields per record. If the number of fields per record is not known, we can set this field to -1
to allow the reader
to determine the number of fields per record.
The ReadAll
method reads all the rows in the CSV file and returns a nested slice of strings. If we were to access a single value, we could do so by using the row and column index:
Read Line by Line
The Read
method on the Reader
type reads a single row from the CSV file and returns a slice of strings. Let’s look at an example:
This example reads the CSV file line by line. The Read
method returns an io.EOF
error when it reaches the end of the file. We can use this error to break out of the loop.
Reading files with different delimiters
Some CSV files use different delimiter characters, such as ;
or |
. To read these files, we can use the Comma
field on the Reader
type to specify the delimiter character. First, let’s create a new CSV file with a different delimiter:
Now let’s read the file using the Comma
field:
Writing CSV Files
A CSV file can be written to using the Writer
type from the encoding/csv
package. The Writer
type has a Write
method that accepts a slice of strings and writes it to the CSV file. Let’s look at an example:
The Flush
method on the Writer
type is used to flush any buffered data to the underlying writer. If we don’t call the Flush
method, the data will not be written to the CSV file.
The Writer
type like the Reader
type has a WriteAll
method that accepts a nested slice of strings and writes it to the CSV file. Let’s look at an example:
Append to CSV Files
Appending is the same way as writing to a CSV file, except that we need to open the file in append mode.
Conclusion
In this article, we looked at how to read and write CSV files in Go. We also looked at how to append data to a CSV file. For more features of the encoding/csv
package, check out the official documentation.