I have a text file data.txt
:
0,0123,"Value 1"
1,0456,"Value 2"
In Go I have defined struct:
type ChangeStatus struct {
Nr1 string
Nr2 string
Category string
}
I am new to Go so I was wondering how can I read that text file and put each text file line into array of ChangeStatus
?
You could use csv.Reader
for that, for example:
func main() {
status := []ChangeStatus{}
f := strings.NewReader(text_file) //replace this with os.Open as needed
//defer f.Close()
r := csv.NewReader(f)
for {
if parts, err := r.Read(); err == nil {
cs := ChangeStatus{parts[0], parts[1], parts[2]}
status = append(status, cs)
} else {
break
}
}
fmt.Printf("%+v
", status)
}