将值类型转换为Map在Golang中的实现方式是什么?

huangapple go评论66阅读模式
英文:

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:

&lt; map[string]string Value &gt;.

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 [&lt; map[string]string Value &gt;] to which I grab the first object in that array. However, I'm not sure how to convert [&lt; map[string]string Value &gt;] into a regular map.

view_args := reflect.ValueOf(&amp;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{&quot;foo&quot;: 1, &quot;bar&quot;: 3}
v := reflect.ValueOf(m)
i := v.Interface()
a := i.(map[string]int)

println(a[&quot;foo&quot;]) // 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 assertiontype 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(&quot;it&#39;s a map, and key \&quot;key\&quot; is&quot;, item[&quot;key&quot;])
case string:
    fmt.Println(&quot;it&#39;s a string:&quot;, item)
default:
    // optional--code that runs if it&#39;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(&quot;unknown type&quot;)
}

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.

huangapple
  • 本文由 发表于 2013年12月21日 08:23:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/20714004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定