I want to create a pointer and use it in every iteration but in the and I get all the same values. So I found the solution using creating the pointer in every iteration. But this approach seems wasting a lot of memory.
Is there a way to reuse the same variable in Go?
Here is my code
func GetSchedules(start, end time.Time, usr *user.User) ([]*Scheduler, error) {
queryStr := []string{"SELECT account_id,link,text,time,image_links from", Table, "where user_id=?"}
var results = make([]*Scheduler, 0)
rows, err := DB.MYSQL_DB.Query(strings.Join(queryStr, " "), usr.Id.Hex())
if nil != err {
return results, err
}
defer rows.Close()
a := new(Scheduler)
for rows.Next() {
cols := []interface{}{&a.AccountId, &a.Link, &a.Text, &a.Time, &a.Images}
fmt.Println(rows.Scan(cols...))
results = append(results, a)
}
return results, nil
}
hereThe problem is with the variable called a although I am scanning new stuff into it in every iteration it just keeps showing the last one and the results slice contains just the last item multiple times
ain every iteration. So which one is the problem?