英文:
Convert Value type to Map in Golang?
问题
我从 "reflect" 包的函数调用中得到了这个返回值:< map[string]string Value >
。想知道我是否可以访问返回值中的实际映射,并且如果可以的话,如何访问?
编辑:
这是我进行调用并返回 Value 对象的地方。它返回 [< map[string]string Value >]
,我从该数组中获取第一个对象。然而,我不确定如何将 [< map[string]string Value >]
转换为普通的映射。
view_args := reflect.ValueOf(&controller_ref).MethodByName(action_name).Call(in)
英文:
I'm getting this return value from a function call in the "reflect" package:
< map[string]string Value >
.
Wondering if I can access the actual map inside the return value and if so, how?
EDIT:
So this is where I'm making the call which returns the Value object.
It returns [< map[string]string Value >]
to which I grab the first object in that array. However, I'm not sure how to convert [< map[string]string Value >]
into a regular map.
view_args := reflect.ValueOf(&controller_ref).MethodByName(action_name).Call(in)
答案1
得分: 21
大多数反射的Value
对象可以使用.Interface()
方法转换回interface{}
值。
在获得这个值之后,你可以将其断言回你想要的映射类型。例如(play):
m := map[string]int{"foo": 1, "bar": 3}
v := reflect.ValueOf(m)
i := v.Interface()
a := i.(map[string]int)
println(a["foo"]) // 1
在上面的示例中,m
是你的原始映射,v
是反射的值。通过Interface
方法获得的接口值i
被断言为map[string]int
类型,并且在最后一行中使用该值。
英文:
Most reflect Value
objects can be converted back to a interface{}
value using the .Interface()
method.
After obtaining this value, you can assert it back to the map you want. Example (play):
m := map[string]int{"foo": 1, "bar": 3}
v := reflect.ValueOf(m)
i := v.Interface()
a := i.(map[string]int)
println(a["foo"]) // 1
In the example above, m
is your original map and v
is the reflected value. The interface value i
, acquired by the Interface
method is asserted to be of type map[string]int
and this value is used as such in the last line.
答案2
得分: 8
将reflect.Value
中的值转换为interface{}
,可以使用iface := v.Interface()
。然后,要访问它,可以使用type assertion或type switch。
如果你知道你得到的是map[string]string
,那么断言就是m := iface.(map[string]string)
。如果有一些可能性,可以使用类型开关来处理它们,代码如下:
switch item := iface.(type) {
case map[string]string:
fmt.Println("它是一个map,键\"key\"的值是", item["key"])
case string:
fmt.Println("它是一个字符串:", item)
default:
// 可选--如果不是上述类型,则执行的代码
// 可以使用reflect来访问对象,如果有意义的话
// 或者根据情况返回错误或抛出异常
fmt.Println("未知类型")
}
当然,这仅适用于在代码中能够列出所有感兴趣的具体类型的情况。如果在编译时不知道可能的类型,你必须使用v.MapKeys()
和v.MapIndex(key)
等方法来更多地处理reflect.Value
,根据我的经验,这需要花费很长时间阅读reflect文档,而且通常会很冗长和棘手。
英文:
To turn the value in a reflect.Value
into an interface{}
, you use iface := v.Interface()
. Then, to access that, you use a type assertion or type switch.
If you know you're getting a map[string]string
the assertion is simply m := iface.(map[string]string)
. If there's a handful of possibilities, the type switch to handle them all looks like:
switch item := iface.(type) {
case map[string]string:
fmt.Println("it's a map, and key \"key\" is", item["key"])
case string:
fmt.Println("it's a string:", item)
default:
// optional--code that runs if it's none of the above types
// could use reflect to access the object if that makes sense
// or could do an error return or panic if appropriate
fmt.Println("unknown type")
}
Of course, that only works if you can write out all the concrete types you're interested out in the code. If you don't know the possible types at compile time, you have to use methods like v.MapKeys()
and v.MapIndex(key)
to work more with the reflect.Value
, and, in my experience, that involves a long time looking at the reflect
docs and is often verbose and pretty tricky.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论