Reputation: 4202
Can I concatenate 2 csv columns into a single struct member with Go CSV?
CSV format is like this.
colA, date, time, colB
A1, 2017-04-14, 09:50:10, B1
A2, 2017-04-14, 09:50:20, B2
I would like to map this CSV into the struct
type MyStruct struct {
ColA string `csv:"colA"`
DateTime string // <- like "2017-04-14 09:50:10"
ColB string `csv:"colB"`
}
How can I do this with Go CSV or another way in Go?
Upvotes: 2
Views: 514
Reputation: 14686
(a) I don't think there is a supported way to do this. However you could implement a custom reader that merges fields into each other. Rather proprietary, I wouldn't recommend it.
(b) Why not simply add a method to MyStruct that returns the merged values?
type MyStruct struct {
ColA string `csv:"colA"`
ColB string `csv:"colB"`
ColC string `csv:"colC"`
}
func (m MyStruct) dateTime() string {
return ColB+ColC
}
(c) Maybe use a little bit of shell-fu to pre-process the CSV before parsing it?
Upvotes: 2