根据这个问题和go代码扫描queryrow到go中现有的map[string]interface{},我试图获取data["id"]的键和值
func login() func(c *lmhttp.context, code int, data interface{}) (int, interface{}) { return func(c *lmhttp.context, code int, data interface{}) (int, interface{}) { map_data := data.(map[string]interface{}) fmt.print(map_data, map_data["id"]) } }
但我总是遇到如下错误,非常感谢您的建议。
interface conversion: interface {} is loginresp, not map[string]interface {}
我还粘贴了我的 response 代码,如下所示:
func (c *Context) Response(data interface{}) { c.result(http.StatusOK, data) }
最后,我通过下面的代码获取值,使用marshal获取json数据,然后map和unmarshal它,
json_str, jsonErr := json.Marshal(data) if json_str != nil { fmt.Printf("%v", jsonErr) } m := make(map[string]interface{}) err := json.Unmarshal([]byte(json_str), &m) if err != nil { fmt.Println(err) fmt.Println("This is ID", m["id"]) }
以上就是Go中如何获取interface{}的值? (接口转换:interface{}是Resp,不是mapinterface{})的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号