Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Parse CSV Data Using The Go Programming Language

Ever found yourself working with comma separated value (CSV) data from a file or other source? This format is easy to generate if you’re working with spreadsheet applications like Google Sheets or Microsoft Excel, and RDBMS applications. So how do we load this data and work with it in an application? More specifically an application built with the Go programming language?

We’re going to see how to take a CSV file and load it into a custom data structure to eventually be printed as JSON within the application.

In many programming languages, working with comma separated value data is often painful. Generally there are libraries that need to be loaded and a lot of work needs to be done. In Golang, CSV packages are already available to use through the encoding/csv package.

Let’s take a look at the following CSV data:

Nic,Raboy,San Francisco,CA
Maria,Raboy,Dublin,CA
Steve,,,

The above is simple data to represent a list of people. It can have more rows and columns, but for this example it won’t make a difference. The goal here is to load this list of people and print it out as JSON data.

Let’s take a look at the following Golang code:

package main

import (
    "bufio"
    "encoding/csv"
    "encoding/json"
    "fmt"
    "io"
    "log"
    "os"
)

type Person struct {
    Firstname string   `json:"firstname"`
    Lastname  string   `json:"lastname"`
    Address   *Address `json:"address,omitempty"`
}

type Address struct {
    City  string `json:"city"`
    State string `json:"state"`
}

func main() {
    csvFile, _ := os.Open("people.csv")
    reader := csv.NewReader(bufio.NewReader(csvFile))
    var people []Person
    for {
        line, error := reader.Read()
        if error == io.EOF {
            break
        } else if error != nil {
            log.Fatal(error)
        }
        people = append(people, Person{
            Firstname: line[0],
            Lastname:  line[1],
            Address: &Address{
                City:  line[2],
                State: line[3],
            },
        })
    }
    peopleJson, _ := json.Marshal(people)
    fmt.Println(string(peopleJson))
}

We can assume the above code exists in a main.go file and the CSV data exists in a people.csv file. Both files should exist at the same location on the computer.

type Person struct {
    Firstname string   `json:"firstname"`
    Lastname  string   `json:"lastname"`
    Address   *Address `json:"address,omitempty"`
}

type Address struct {
    City  string `json:"city"`
    State string `json:"state"`
}

The people data would best be loaded into the above structures. Each property of the structures have a JSON annotation which will allow for easy printing to JSON after it had been loaded.

After opening the comma separated value file, each line is looped through and each column of each line is appended to a people slice. The comma separated value columns are a slice, so you can get the total column count if you wanted to.

Conclusion

Loading data from comma separated value files in the Go programming language is not difficult because of the included package. The data loaded can easily be transformed into something more manageable.

The encoding/csv package has a lot of options such as trimming columns that can make your life even easier.

A video version of this tutorial can be found below.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

Search

Follow Us

Subscribe

Subscribe to my newsletter for monthly tips and tricks on subjects such as mobile, web, and game development.

The Polyglot Developer
The Polyglot Developer

Support This Site

Close

Subscribe To Our Newsletter

Stay up to date on the latest in web, mobile, and game development, plus receive exclusive content by subscribing to The Polyglot Developer newsletter.

Unsubscribe at any time without hassle.