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
+5.8k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+16.5k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+12.1k Golang : Validate email address
+17.1k Golang : Check if IP address is version 4 or 6
+7.9k Golang : How To Use Panic and Recover
+4.8k Golang : Display packages names during compilation
+5.7k Golang : List all packages and search for certain package
+26.5k Golang : Force your program to run with root permissions
+30.5k Golang : Interpolating or substituting variables in string examples
+11.2k Golang : Find age or leap age from date of birth example
+9.6k Golang : Sort and reverse sort a slice of integers
+17.2k Golang : Find smallest number in array