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
|
package main
import (
"encoding/csv"
"errors"
"fmt"
"log"
"os"
"github.com/jszwec/csvutil"
)
type Participant struct {
LastName string `csv:"lastname"`
FirstName string `csv:"firstname"`
DateOfBirth string `csv:"dateofbirth"`
Gender string `csv:"gender"`
Nation string `csv:"nation"`
Region string `csv:"region"`
Club string `csv:"club"`
}
func parseOphardtInput(fileName string) ([]Participant, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("opening input file '%s': %w", fileName, err)
}
encReader, err := getEncodedReader(f)
if err != nil {
return nil, fmt.Errorf("cannot determine encoding of file '%s': %w", fileName, err)
}
csvReader := csv.NewReader(encReader)
csvReader.Comma = ';'
dec, err := csvutil.NewDecoder(csvReader)
if err != nil {
return nil, fmt.Errorf("reading from file '%s': %w", fileName, err)
}
dec.DisallowMissingColumns = true
var participants []Participant
if err = dec.Decode(&participants); err != nil {
return nil, fmt.Errorf("decoding file '%s': %w", fileName, err)
}
return participants, nil
}
func run() error {
if len(os.Args) <= 1 {
return errors.New("need filename to start with")
}
fileName := os.Args[1]
p, err := parseOphardtInput(fileName)
if err != nil {
return err
}
fmt.Print(p)
return nil
}
func main() {
if err := run(); err != nil {
log.Fatalf("An error occured: %v", err)
}
}
|