go - Golang convert map[string]*[]interface{} to slice or array of other type -


i have following

type results map[string]*[]interface{}  var users *[]models.user users = getusers(...)  results := results{} results["users"] = users 

later, id able grab users map , cast *[]models.user

i having hard time figuring out right way this. id following, not work.

var userresults *[]models.user userresults = (*results["users").(*[]models.user) 

any idea on how this?

here comments on code besides conversion (which addressed @ end).

  • there no real need of using pointer slice in case, since slices header values pointing underlying array. slice values work references underlying array.
  • since interface{} variable can reference values of kind (including slices), there no need use slice of interface{} in result, can define type results map[string]interface{}.

having said this, here's how modified code like:

var users []user users = getusers()  results := results{} results["users"] = users  fmt.println(results)  var userresults []user userresults = results["users"].([]user)  fmt.println(userresults) 

you can find complete code in playground:

https://play.golang.org/p/zl4ikwy97j


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -