var newR[] struct {
id string
eventid string
excel_id string
userid string
hallid string
}
i := 0
for rows.Next() {
var id, eventid, excel_id, userid, hallid string
err = rows.Scan(&id, &eventid, &excel_id, &userid, &hallid)
// Here is what I want to do
newR[i].id = id
newR[i].eventid = eventid
newR[i].excel_id = excel_id
newR[i].userid = userid
newR[i].hallid = hallid
i++
}
最终,我收到一条错误消息“运行时错误:索引超出范围”
在/myapp/app/controllers/app.go中(围绕第122行)
newR[i].id = id
任何建议或提示将有所帮助。谢谢。
最佳答案
您不需要为每个字段创建局部变量,只需创建一个结构并使用它来将数据读入并使用 slice 来累积结果:
// struct definition must be out of functions body
type newR struct {
id string
eventid string
excel_id string
userid string
hallid string
}
var newRs []newR
for rows.Next() {
var current newR
err = rows.Scan(¤t.id, ¤t.eventid, ¤t.excel_id, ¤t.userid, ¤t.hallid)
newRs = append(newRs, r)
}
而且最好检查
rows.Next()
和rows.Scan(...)
之后的错误。关于mysql - 去郎 map 多结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45316699/