mysql - Go lang map multi result -
var newr[] struct { id string eventid string excel_id string userid string hallid string } := 0 rows.next() { var id, eventid, excel_id, userid, hallid string err = rows.scan(&id, &eventid, &excel_id, &userid, &hallid) // here want newr[i].id = id newr[i].eventid = eventid newr[i].excel_id = excel_id newr[i].userid = userid newr[i].hallid = hallid i++ }
eventually got 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 helps. thanks.
you not need create local variables each field, create struct , use read data , use slice accumulate results:
// struct definition must out of functions body type newr struct { id string eventid string excel_id string userid string hallid string } var newrs []newr rows.next() { var current newr err = rows.scan(¤t.id, ¤t.eventid, ¤t.excel_id, ¤t.userid, ¤t.hallid) newrs = append(newrs, r) }
and better check errors after rows.next()
, rows.scan(...)
.
Comments
Post a Comment