I'm trying to read contents from a csv file. I have the following but when I run the code it only prints the last line of the csv file.
func main() {
f, err := os.Open("data.csv")
if err != nil {
log.Fatal(err)
}
defer f.Close()
r := csv.NewReader(f)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Println(record)
}
}
// Returns
10 3311 74209 OM209] site
What am I doing wrong?
data.csv
Item,clientid,ident,site
1,2493,83982,OM221
2,2509,85764,OM201
3,2535,70264,OM205
4,2608,70648,OM205
5,2766,83647,OM304
6,2871,69400,OM201
7,2933,80024,OM201
8,2994,77827,OM301
9,3206,73315,OM201
10,3311,74209,OM209