drbhjey445647 2013-01-03 16:10
浏览 266
已采纳

Reflection.Value.MapIndex()返回与reflect.ValueOf()不同的值

According to the reflect documentation reflect.Value.MapIndex() should return a reflect.Value which represents the Value of the data stored at a specific key of a map. So my understanding would be that the following two expressions should be identical. In the first case we are getting the result from MapIndex(). In the second we are getting the result from MapIndex() getting it's underlying data and then doing a reflect.ValueOf() on that.

reflect.ValueOf(map).MapIndex("Key")
reflect.ValueOf(reflect.ValueOf(map).MapIndex("Key").Interface())

Why is the additional reflect.ValueOf() required?

Example Code:

package main

import "fmt"
import "reflect"

func main() {
    test := map[string]interface{}{"First": "firstValue"}
    Pass(test)
}

func Pass(d interface{}) {
    mydata := reflect.ValueOf(d).MapIndex(reflect.ValueOf("First"))
    fmt.Printf("Value: %+v 
", mydata.Interface())
    fmt.Printf("Kind: %+v 
", mydata.Kind())
    fmt.Printf("Kind2: %+v 
", reflect.ValueOf(mydata.Interface()).Kind())
}

Go Play: http://play.golang.org/p/TG4SzrtTf0

  • 写回答

2条回答 默认 最新

  • dou4381 2013-01-04 04:32
    关注

    After thinking about this for a while this falls under the duh category. It has to do with the nature of interfaces in Go, which are reference objects which point to other things. I have explicitly declared my map to be map[string]interface{} meaning that the value at the location of each key is an interface{}, not a string, so I really shouldn't be surprised to receive a reflect.Value which holds an interface{}.

    The additional reflect.ValueOf() dives one layer deeper to gain the underlying Value of the interface{}. I created two examples, both of which I believe confirm this behavior.

    Example using a map[string]Stringer custom interface: http://play.golang.org/p/zXCn9Fce3Q

    package main
    
    import "fmt"
    import "reflect"
    
    type Test struct {
        Data string
    }
    
    func (t Test) GetData() string {
        return t.Data
    }
    
    type Stringer interface {
        GetData() string
    }
    
    func main() {
        test := map[string]Stringer{"First": Test{Data: "testing"}}
        Pass(test)
    }
    
    func Pass(d interface{}) {
        mydata := reflect.ValueOf(d).MapIndex(reflect.ValueOf("First"))
        fmt.Printf("Value: %+v 
    ", mydata.Interface())
        fmt.Printf("Kind: %+v 
    ", mydata.Kind())
        fmt.Printf("Kind2: %+v 
    ", reflect.ValueOf(mydata.Interface()).Kind())
    }
    

    returns

    Value: {Data:testing} 
    Kind: interface 
    Kind2: struct
    

    Example using a map[string]string: http://play.golang.org/p/vXuPzmObgN

    package main
    
    import "fmt"
    import "reflect"
    
    func main() {
        test := map[string]string{"First": "firstValue"}
        Pass(test)
    }
    
    func Pass(d interface{}) {
        mydata := reflect.ValueOf(d).MapIndex(reflect.ValueOf("First"))
        fmt.Printf("Value: %+v 
    ", mydata.Interface())
        fmt.Printf("Kind: %+v 
    ", mydata.Kind())
        fmt.Printf("Kind2: %+v 
    ", reflect.ValueOf(mydata.Interface()).Kind())
    }
    

    returns

    Value: firstValue 
    Kind: string 
    Kind2: string 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集