37 lines
603 B
Go
37 lines
603 B
Go
|
package csvparser
|
||
|
|
||
|
import (
|
||
|
"encoding/csv"
|
||
|
"io"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
// CsvLine holds a single line from the source CSV file
|
||
|
type CsvLine struct {
|
||
|
Code string
|
||
|
Name string
|
||
|
}
|
||
|
|
||
|
// ParseFile parses a given CSV file and returns a slice of CsvLine
|
||
|
func ParseFile(file string) ([]CsvLine, error) {
|
||
|
csvfile, err := os.Open(file)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
r := csv.NewReader(csvfile)
|
||
|
var lines []CsvLine
|
||
|
|
||
|
for {
|
||
|
record, err := r.Read()
|
||
|
if err == io.EOF {
|
||
|
break
|
||
|
}
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
lines = append(lines, CsvLine{Code: record[0], Name: record[1]})
|
||
|
}
|
||
|
return lines, nil
|
||
|
}
|