-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.go
122 lines (66 loc) · 2.89 KB
/
doc.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// © 2017 Osvaldo Gago
/*
The Go package simplecsv is a simple mini-library to handle csv files. I'm building it to help me writing small command line scripts. Maybe it's useful to someone else as well.
Some notes:
- all read methods return the value in the csv and a second true/false value that is true if the value exists
- all write methods that change the csv return the changed csv and a true/false value if the operation was sucessful
- all cells are strings
- Simplecsv works with comma separated csv files.
* CSV FILE *
READ
Reads file and parses as a SimpleCsv object. `fileRead` is false if there's an error reading the file or parsing the CSV.
var x simplecsv.SimpleCsv
x, fileRead = simplecsv.ReadCsvFile("my1file.csv")
CREATE
Create empty file and define cssv headers:
var u simplecsv.SimpleCsv
u = simplecsv.CreateEmpyCsv([]string{"Age", "Gender", "ID"})
WRITE
Write the SimpleCsv object to my2file.csv. If there's an error, `wasWritten` is false.
wasWritten := u.WriteCsvFile("my2file.csv")
* HEADERS *
The cells of the first row are considered headers.
GET
Get all headers:
headers := x.GetHeaders()
Get header at position one (second position as it starts from 0):
headerName, headerExists := x.GetHeader(1)
Get header position: (it returns `-1` if the header does not exist)
position := x.GetHeaderPosition("Gender")
RENAME
Rename header: (old header, new header)
x, headerExists := x.RenameHeader("ID", "IDnumber")
`headerExists` is false if the old header does not exist.
* ROWS *
GET
Get number of rows:
numberOfRows := x.GetNumberRows()
Get second row:
row, rowExists := x.GetRow(1)
Get second row as a map:
row, rowExists := x.GetRowAsMap(1)
ADD
Add a slice to a row. The slice must have the same size as the CSV number of columns. If not wasSucessful is false.
x, wasSucessful = x.AddRow([]string{"24", "M", "2986732"})
Add row from map: (If the map keys don't exist as columns, the value will be discarded. If a key does not exist, it will create empty cells.)
mymap := make(map[string]string)
mymap["Age"] = "62"
mymap["Gender"] = "F"
mymap["ID"] = "6463246"
x, wasAdded = x.AddRowFromMap(mymap)
SET
Set second row (1) from a slice. The length of the slice must be the same as the number of columns and the row must already exist. If there’s an error `wasSet` is false.
x, wasSet = x.SetRow(1, []string{"45", "F", "8356138"})
Set second row from map: If the map keys don't exist as columns, the value will be discarded. If a key does not exist, it will create empty cells.)
mymap2 := make(map[string]string)
mymap2["Age"] = "62"
mymap2["Gender"] = "F"
mymap2["ID"] = "6463246"
x, wasAdded = x.SetRowFromMap(1, mymap2)
DELETE
Delete second row: (If the row number is invalid, `wasDeleted` is false)
x, wasDeleted = x.DeleteRow(1)
* CELLS *
.
*/
package simplecsv