How can I fill the todos-Object with a for-loop?
type Row struct {
Name string
Completed bool
Due time.Time
Rcount string
}
type Rows []Row
todos := Rows{
Row{Name: "Write presentation"},
Row{Name: "Host meetup"},
}
I think you are looking for the builtin function append
Note that it is normally used in combination with an assignment, because it may have to allocate additional memory. A zero value slice works just fine, no need to call make.
steps := []string{"write program", "???", "profit"}
var rows []Row
for _, tasks := range steps {
rows = append(rows, Row{Name: tasks})
}
If you want to loop over a sqlite3 query result, your loop will look different, but the x = append(x, ...)
pattern will stay the same
If you know in advance how big your slice is going to be, explicit initialization with make will be more efficient.
var rows = make([]Row, len(steps))
for i, tasks := range steps {
rows[i] = Row{Name: tasks}
}
The question is a little hard to follow but try starting out with something following this pattern (error handling omitted for brevity):
rows, _ := db.Query(string, args...)
var Rows []Row
for rows.Next() {
var r Row
rows.Scan(&r.Name, &r.Completed, &r.Due, &r.Rcount)
Rows = append(Rows, r)
}
If you can clarify the question perhaps we can provide better answers