Mysql Go lang地图多结果
最终我得到一个错误消息“运行时错误:索引超出范围” In/myapp/app/controllers/app.go(绕第122行)Mysql Go lang地图多结果,mysql,dictionary,go,Mysql,Dictionary,Go,最终我得到一个错误消息“运行时错误:索引超出范围” In/myapp/app/controllers/app.go(绕第122行) 任何建议或提示都会有所帮助。谢谢。使用append添加新元素并初始化尚未分配的片 newR[i].id = id 很抱歉没有进行fmt或测试,但我是从手机上键入的。您不需要为每个字段创建一个局部变量,只需创建一个结构并使用它将数据读入并使用一个切片来累积结果: newElement := struct { id string eventid st
任何建议或提示都会有所帮助。谢谢。使用append添加新元素并初始化尚未分配的片
newR[i].id = id
很抱歉没有进行fmt或测试,但我是从手机上键入的。您不需要为每个字段创建一个局部变量,只需创建一个结构并使用它将数据读入并使用一个切片来累积结果:
newElement := struct {
id string
eventid string
excel_id string
userid string
hallid string
} {
id: id,
eventid: eventid,
excel_id: excel_id,
userid: userid,
hallid: hallid,
}
newR = append(newR, newElement)
此外,最好在
行.Next()
和行.Scan(…)
之后检查错误,因为您缺少问题中的关键信息,即如何创建切片。我猜您将它设置得太小,无法存储所需的数据量。
newElement := struct {
id string
eventid string
excel_id string
userid string
hallid string
} {
id: id,
eventid: eventid,
excel_id: excel_id,
userid: userid,
hallid: hallid,
}
newR = append(newR, newElement)
// 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)
}