dopuzf0898 2017-07-26 03:01
浏览 15
已采纳

去郎地图多结果

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++
}

Eventually I got an error msg "runtime error: index out of range" In /myapp/app/controllers/app.go (around line 122)

newR[i].id = id

Any suggestions or tips will helps. Thanks.

  • 写回答

2条回答 默认 最新

  • dozr162106 2017-07-26 06:50
    关注

    You do not need to create a local variables for each field, just create a struct and use it to read data into and use a slice to accumulate results:

    // 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)
    }
    

    And also it is better to check for errors after rows.Next() and rows.Scan(...).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Fluent udf 编写问题
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突