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

and better check errors after rows.next() , rows.scan(...).


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -