Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Go lang地图多结果_Mysql_Dictionary_Go - Fatal编程技术网

Mysql Go lang地图多结果

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

最终我得到一个错误消息“运行时错误:索引超出范围” In/myapp/app/controllers/app.go(绕第122行)


任何建议或提示都会有所帮助。谢谢。

使用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(&current.id, &current.eventid, &current.excel_id, &current.userid, &current.hallid)
    newRs = append(newRs, r)
}