Read a CSV File into a Struct

· 213 words · 1 minute read

We have a similar post on writing data to a CSV file. This post however, focuses on the simple task of taking data from a csv file and converting it into data we can work with.

The first part it to open the file, then we read it into the lines variable and finally we loop through the lines and we turn them into CsvLine objects - obviously in a real scenario we should use a more descriptive naming.

package main

import (

type CsvLine struct {
    Column1 string
    Column2 string

func main() {

    lines, err := ReadCsv("example.csv")
    if err != nil {

    // Loop through lines & turn into object
    for _, line := range lines {
        data := CsvLine{
            Column1: line[0],
            Column2: line[1],
        fmt.Println(data.Column1 + " " + data.Column2)

// ReadCsv accepts a file and returns its content as a multi-dimentional type
// with lines and each column. Only parses to string type.
func ReadCsv(filename string) ([][]string, error) {

    // Open CSV file
    f, err := os.Open(filename)
    if err != nil {
        return [][]string{}, err
    defer f.Close()

    // Read File into a Variable
    lines, err := csv.NewReader(f).ReadAll()
    if err != nil {
        return [][]string{}, err

    return lines, nil

read a csv file into a struct

Image of Author Edd Turtle

Author:  Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code. He loves coding, but also enjoys cycling and camping in his spare time.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below - Thanks for reading!