-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.go
More file actions
52 lines (46 loc) · 1017 Bytes
/
io.go
File metadata and controls
52 lines (46 loc) · 1017 Bytes
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package simplecsv
import (
"encoding/csv"
"os"
)
// SimpleCsv is the type for simple csv
type SimpleCsv [][]string
// CreateEmpyCsv creates an empty CSV with the headers passed as a slice
func CreateEmpyCsv(columnNames []string) SimpleCsv {
a := make([][]string, 1)
a[0] = make([]string, len(columnNames))
a[0] = columnNames
return a
}
// ReadCsvFile reads a file and returns a [][]string slice
func ReadCsvFile(filename string) (SimpleCsv, bool) {
ok := true
file, err := os.Open(filename)
if err != nil {
ok = false
}
defer file.Close()
reader := csv.NewReader(file)
reader.Comma = ','
// lineCount := 0
allRecords, err := reader.ReadAll()
if err != nil {
ok = false
}
return allRecords, ok
}
// WriteCsvFile writes the slice to a file
func (s SimpleCsv) WriteCsvFile(filename string) bool {
ok := true
file, err := os.Create(filename)
if err != nil {
ok = false
}
w := csv.NewWriter(file)
w.WriteAll(s)
if err := w.Error(); err != nil {
ok = false
}
w.Flush()
return ok
}