Go语言中的map可以返回多个结果。

huangapple go评论110阅读模式
英文:

Go lang map multi result

问题

以下是翻译好的内容:

  1. var newR []struct {
  2. id string
  3. eventid string
  4. excel_id string
  5. userid string
  6. hallid string
  7. }
  8. i := 0
  9. for rows.Next() {
  10. var id, eventid, excel_id, userid, hallid string
  11. err = rows.Scan(&id, &eventid, &excel_id, &userid, &hallid)
  12. // 这里是我想要做的
  13. newR = append(newR, struct {
  14. id string
  15. eventid string
  16. excel_id string
  17. userid string
  18. hallid string
  19. }{})
  20. newR[i].id = id
  21. newR[i].eventid = eventid
  22. newR[i].excel_id = excel_id
  23. newR[i].userid = userid
  24. newR[i].hallid = hallid
  25. i++
  26. }
  27. 最终我得到了一个错误消息 "runtime error: index out of range"
  28. /myapp/app/controllers/app.go大约在第122
  29. newR[i].id = id
  30. 有什么建议或提示将会有所帮助谢谢
英文:
  1. var newR[] struct {
  2. id string
  3. eventid string
  4. excel_id string
  5. userid string
  6. hallid string
  7. }
  8. i := 0
  9. for rows.Next() {
  10. var id, eventid, excel_id, userid, hallid string
  11. err = rows.Scan(&id, &eventid, &excel_id, &userid, &hallid)
  12. // Here is what I want to do
  13. newR[i].id = id
  14. newR[i].eventid = eventid
  15. newR[i].excel_id = excel_id
  16. newR[i].userid = userid
  17. newR[i].hallid = hallid
  18. i++
  19. }

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

  1. newR[i].id = id

Any suggestions or tips will helps. Thanks.

答案1

得分: 0

使用append函数来添加一个新元素并初始化一个尚未分配的切片。

  1. newElement := struct {
  2. id string
  3. eventid string
  4. excel_id string
  5. userid string
  6. hallid string
  7. } {
  8. id: id,
  9. eventid: eventid,
  10. excel_id: excel_id,
  11. userid: userid,
  12. hallid: hallid,
  13. }
  14. newR = append(newR, newElement)
  15. ...

抱歉我没有使用fmt或测试它,但我是从我的手机上输入的。

英文:

Use append to add a new element and initialize a not yet allocated slice.

  1. newElement := struct {
  2. id string
  3. eventid string
  4. excel_id string
  5. userid string
  6. hallid string
  7. } {
  8. id: id,
  9. eventid: eventid,
  10. excel_id: excel_id,
  11. userid: userid,
  12. hallid: hallid,
  13. }
  14. newR = append(newR, newElement)

...

Sorry for not fmt or test it out but I typed this from my mobile.

答案2

得分: 0

你不需要为每个字段创建本地变量,只需创建一个结构体并使用它来读取数据,并使用切片来累积结果:

  1. // 结构体定义必须在函数体外部
  2. type newR struct {
  3. id string
  4. eventid string
  5. excel_id string
  6. userid string
  7. hallid string
  8. }
  9. var newRs []newR
  10. for rows.Next() {
  11. var current newR
  12. err = rows.Scan(&current.id, &current.eventid, &current.excel_id, &current.userid, &current.hallid)
  13. newRs = append(newRs, current)
  14. }
  15. // 在 `rows.Next()` 和 `rows.Scan(...)` 之后最好检查错误

请注意,我已经将代码中的 & 替换为 &,因为它是 HTML 实体编码的表示形式。

英文:

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:

  1. // struct definition must be out of functions body
  2. type newR struct {
  3. id string
  4. eventid string
  5. excel_id string
  6. userid string
  7. hallid string
  8. }
  9. var newRs []newR
  10. for rows.Next() {
  11. var current newR
  12. err = rows.Scan(&current.id, &current.eventid, &current.excel_id, &current.userid, &current.hallid)
  13. newRs = append(newRs, r)
  14. }

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

huangapple
  • 本文由 发表于 2017年7月26日 11:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/45316699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定