Unmarshal/Load CSV record into struct in Go
This is just a slight variation to the previous go read csv file tutorial. In this tutorial, we will use our own data structure and try to unmarshal(load) the csv data into the data structure.
1st November 2014 : UPDATED codes to correct earlier mistake as pointed out by Christian von Kietzell below
csv2struct.go
package main
import (
"encoding/csv"
"fmt"
"os"
)
type TestRecord struct {
Email string
Date string
}
func main() {
csvfile, err := os.Open("somecsvfile.csv")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer csvfile.Close()
reader := csv.NewReader(csvfile)
reader.FieldsPerRecord = -1
rawCSVdata, err := reader.ReadAll()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// sanity check, display to standard output
for _, each := range rawCSVdata {
fmt.Printf("email : %s and timestamp : %s\n", each[0], each[1])
}
// now, safe to move raw CSV data to struct
var oneRecord TestRecord
var allRecords []TestRecord
for _, each := range rawCSVdata {
oneRecord.Email = each[0]
oneRecord.Date = each[1]
allRecords = append(allRecords, oneRecord)
}
// second sanity check, dump out allRecords and see if
// individual record can be accessible
fmt.Println(allRecords)
fmt.Println(allRecords[2].Email)
fmt.Println(allRecords[2].Date)
}
The CSV file contains the following data
more somecsvfile.csv
"jenniferlcl@*****.com","2012-07-03 18:38:06"
"norazlinjumali@*****.com","2010-06-26 19:46:08"
"wilfred5571@*****.com","2010-07-02 21:49:55"
"nas_kas81@*****.com","2010-07-06 12:49:31"
"tammyu3622@*****.com","2010-07-06 13:55:21"
"wakrie@*****.com","2012-03-02 11:00:59"
"yst.shirin@*****.com","2010-07-07 10:19:11"
"annl_107@*****.com","2010-07-07 20:55:59"
"jen_5831@*****.com","2010-07-07 21:12:27"
"hsheyli@*****.com","2011-09-07 00:39:11"
and executing go run csv2struct.go
will produce the following output :
email : jenniferlcl@*****.com and timestamp : 2012-07-03 18:38:06
email : norazlinjumali@*****.com and timestamp : 2010-06-26 19:46:08
email : wilfred5571@*****.com and timestamp : 2010-07-02 21:49:55
email : nas_kas81@*****.com and timestamp : 2010-07-06 12:49:31
email : tammyu3622@*****.com and timestamp : 2010-07-06 13:55:21
email : wakrie@*****.com and timestamp : 2012-03-02 11:00:59
email : yst.shirin@*****.com and timestamp : 2010-07-07 10:19:11
email : annl_107@*****.com and timestamp : 2010-07-07 20:55:59
email : jen_5831@*****.com and timestamp : 2010-07-07 21:12:27
email : hsheyli@*****.com and timestamp : 2011-09-07 00:39:11
[{jenniferlcl@*****.com 2012-07-03 18:38:06} {norazlinjumali@*****.com 2010-06-26 19:46:08} {wilfred5571@*****.com 2010-07-02 21:49:55} {naskas81@*****.com 2010-07-06 12:49:31} {tammyu3622@*****.com 2010-07-06 13:55:21} {wakrie@*****.com 2012-03-02 11:00:59} {yst.shirin@*****.com 2010-07-07 10:19:11} {annl107@*****.com 2010-07-07 20:55:59} {jen_5831@*****.com 2010-07-07 21:12:27} {hsheyli@*****.com 2011-09-07 00:39:11}]
wilfred5571@*****.com
2010-07-02 21:49:55
Hope this tutorial is helpful to you on learning how to unmarshal data from file to data structure in Go.
See also : Golang : How to read CSV file
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+18.6k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+14.1k Golang : File path independent of Operating System
+5k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+12.3k Golang : How to check if IP address is in range
+7.5k Golang : Qt Yes No and Quit message box example
+11.4k How to automatically restart your crashed Golang server
+16.5k Golang : Write file with io.WriteString
+8.1k Javascript : Read/parse JSON data from HTTP response
+17.1k Golang : Clearing slice
+5.5k Golang : Of hash table and hash map
+31.8k Golang : Upload and download file to/from AWS S3
+7.6k Golang : Eroding and dilating image with OpenCV example