Managing CSV with Golang
Managing CSV with Golang
Read and Write

What is a CSV file?
The CSV are a type of file with a simple format to represent data as a table, where the columns are separated by commas and the rows by a new line.
Example:
myFile.csv
id,name,lastname,username,organization
0001,Jon,Pan,Jon999,Amazon
0002,Jack,Jonsson,Jackjon,Amazon
0003,Antony,King,KinKong007,Google
Now that we know what a CSV is, it is time to play with a couple of examples to manage the data like writing and reading the content.
Read
For this example we going to need to create the following files in our example project:

- The sample
users_to_import.csv
defines inside the following:
id,name,lastname,username,organization
0001a,Jon,Pan,Jon999,Amazon
0002b,Jack,Jonsson,Jackjon,Amazon
0003c,Antony,King,KinKong007,Google
- And now let us define the code of the main. (In this case, we going to use only the main file).
- For this example, we going to use the
encoding/csv
package. - We need to define a structure to map the CSV values.
main.go
package main
import (
"encoding/csv"
"fmt"
"os"
)
type user struct {
Id string
Name string
Lastname string
Username string
Organization string
}
func main() {
var users []user
file, err := os.Open("files/users_to_import.csv")
if err != nil {
fmt.Println(err)
}
defer file.Close()
reader := csv.NewReader(file)
csvRecords, err := reader.ReadAll()
if err != nil {
panic(err)
}
for i := 1; i < len(csvRecords); i++ {
users = append(users, user{
Id: csvRecords[i][0],
Name: csvRecords[i][1],
Lastname: csvRecords[i][2],
Username: csvRecords[i][3],
Organization: csvRecords[i][4],
})
}
fmt.Println(users)
}
output:
[{0001a Jon Pan Jon999 Amazon} {0002b Jack Jonsson Jackjon Amazon} {0003c Antony King KinKong007 Google}][{0001a Jon Pan Jon999 Amazon} {0002b Jack Jonsson Jackjon Amazon} {0003c Antony King KinKong007 Google}]
Write
Using the same project structure with the main and a folder named files wi going to start to define the needed code to write values in a CSV file.

For this example, we going to simulate an export of users to a CSV file.
- We need to define the golang values to export
- Create the file
- Create CSV writer
- Write the content
For the write example we going to see two ways to write in a csv file, the first one is writing all the content at time and the other one is write line by line.
main.go
Option 1) Write all the elements in the array.
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
)
type user struct {
Id string
Name string
Lastname string
Username string
Organization string
}
func main() {
// some sample data
usersToExport := []user{
{
Id: "097812-abc",
Name: "Jack",
Lastname: "Daniels",
Username: "jd007",
Organization: "Google",
},
{
Id: "097812-abc",
Name: "Jack",
Lastname: "Daniels",
Username: "jd007",
Organization: "Google",
},
}
fmt.Println("data to export: ", usersToExport)
// create the file to write the values
file, err := os.Create("files/export-data.csv")
defer file.Close()
if err != nil {
log.Fatalln("failed to open file", err)
panic(err)
}
// create csv writer
w := csv.NewWriter(file)
defer w.Flush()
csvHeader := []string{"id", "name", "lastname", "username", "organization"}
var csvData [][]string
csvData = append(csvData, csvHeader)
for _, user := range usersToExport {
csvData = append(csvData, []string{user.Id, user.Name, user.Lastname, user.Username, user.Organization})
}
w.WriteAll(csvData)
}
output file named export-data.csv
:
id,name,lastname,username,organization
097812-abc,Jack,Daniels,jd007,Google
097812-abc,Jack,Daniels,jd007,Google
main.go
Option 2) Write elements one by one, This options works fine when we have multiples goroutines writing in parallel in the same csv file:
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
)
type user struct {
Id string
Name string
Lastname string
Username string
Organization string
}
func main() {
// some sample data
usersToExport := []user{
{
Id: "097812-abc",
Name: "Jack",
Lastname: "Daniels",
Username: "jd007",
Organization: "Google",
},
{
Id: "097812-abc",
Name: "Jack",
Lastname: "Daniels",
Username: "jd007",
Organization: "Google",
},
}
fmt.Println("data to export: ", usersToExport)
// create the file to write the values
file, err := os.Create("files/export-data.csv")
defer file.Close()
if err != nil {
log.Fatalln("failed to open file", err)
panic(err)
}
// create csv writer
w := csv.NewWriter(file)
defer w.Flush()
csvHeader := []string{"id", "name", "lastname", "username", "organization"}
var csvData [][]string
csvData = append(csvData, csvHeader)
w.WriteAll(csvData)
for _, user := range usersToExport {
err := w.Write([]string{user.Id, user.Name, user.Lastname, user.Username, user.Organization})
if err != nil {
log.Fatalln("error writing record to file", err)
}
}
}
output file named export-data.csv
:
id,name,lastname,username,organization
0001a,Jon,Pan,Jon999,Amazon
0002b,Jack,Jonsson,Jackjon,Amazon
0003c,Antony,King,KinKong007,Google
Cool! … As you can see use CSV files with golang is easy and you could use this code as a base code nd start doing your implementations.
Thanks for reading and keep you learning!