-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.go
35 lines (29 loc) · 772 Bytes
/
csv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"encoding/csv"
"os"
)
func getRows(file string) [][]string {
f, err := os.Open(file + ".csv")
checkError("Cannot open file", err)
defer f.Close()
rows, err := csv.NewReader(f).ReadAll()
checkError("Cannot get rows file: ", err)
return rows
}
func addNewRow(rows [][]string, newRow []string) [][]string {
newRows := append(rows, newRow)
return newRows
}
func writeChanges(file string, rows [][]string) {
f, err := os.OpenFile(file+".csv", os.O_WRONLY, os.ModeAppend)
checkError("Couldn't save changes: ", err)
err = csv.NewWriter(f).WriteAll(rows)
checkError("Could not write changes: ", err)
f.Close()
}
func writeCSV(file string, data []string) {
rows := getRows(file)
rows = addNewRow(rows, data)
writeChanges(file, rows)
}