-
Notifications
You must be signed in to change notification settings - Fork 22
/
parser.go
41 lines (34 loc) · 830 Bytes
/
parser.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
package main
import (
"encoding/csv"
"fmt"
"os"
)
func ParseCsvFile(filePath string) ([][]string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("error opening CSV file: %v", err)
}
defer file.Close()
reader := csv.NewReader(file)
records, err := reader.ReadAll()
if err != nil {
return nil, fmt.Errorf("error reading CSV file: %v", err)
}
return records, nil
}
func SaveWinners(filePath string, winners [][]string) error {
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("error creating CSV file: %v", err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
for _, winner := range winners {
if err := writer.Write(winner); err != nil {
return fmt.Errorf("error writing winner to CSV file: %v", err)
}
}
return nil
}