简体   繁体   English

golang无法反映到map [interface {}] interface {}

[英]golang can't reflect to map[interface{}]interface{}

My original problem is I want to parse URL.Values to a generic type (map[interface{}]interface{}) edit/add some values then convert it to JSON string and put it to PostgreSQL JSON column. 我最初的问题是我想将URL.Values解析为通用类型(map [interface {}] interface {})编辑/添加一些值,然后将其转换为JSON字符串并将其放入PostgreSQL JSON列。

I tried this code to parse it but content seems to be null whereas err is false. 我尝试使用此代码来解析它,但content似乎为null,而err为false。 request.URL.Query() prints a nice map object. request.URL.Query()打印一个漂亮的地图对象。

v := reflect.ValueOf(request.URL.Query())
i := v.Interface()
content, err := i.(map[interface{}]interface{})

// Do some operations

jsonString, _ := json.Marshal(content)

// Add to DB

Why is it null? 为什么它为空? Also am I thinking too generic? 我也觉得太通用了吗?

content, err := i.(map[interface{}]interface{}) , this isn't a cast, it's a type assertion. content, err := i.(map[interface{}]interface{}) ,这不是强制类型转换,而是类型断言。 You're saying (asserting) that interface is of type map[interface{}]interface{} , it's not. 您说的是(断言)接口的类型为map[interface{}]interface{} ,不是。 It's of type map[string][]string . 类型为map[string][]string You get null as the value because it fails. 您会得到null作为值,因为它失败了。 I highly doubt error is false. 我高度怀疑error是错误的。

Are you thinking too generic? 您是否认为过于笼统? Of course you are. 当然可以 I can't think of any reason why the collections type needs to change... Append what you want to it, write it to your db. 我想不出集合类型需要更改的任何原因...追加您想要的内容,将其写入数据库。 There's nothing preventing that afaik? 没有什么可以防止这种afaik?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM