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 ofinterface{}
inresult
, can definetype 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:
Comments
Post a Comment