I have a struct with fields containing slices like so, and now I want to collect the columns from a sql-query into these slices
type StructOfSlices struct {
Column1 []string
Column2 []string
}
Is there an easier/more scalable/efficient way to do it than the below?
Here I first create a "single" version of the struct...
type StructSingle struct {
Column1 string
Column2 string
}
var s StructOfSlices
rows, _ := db.Query("SELECT column1, column2 FROM table")
...and then scan each individual single struct...
for rows.Next() {
var single StructSingle
db.Scan(&single.Column1, &single.Column2)
s.Column1 = append(s.Column1, single.Column1)
s.Column2 = append(s.Column2, single.Column2)
}
...and then append to the slice struct