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 \n", mydata.Interface())
fmt.Printf("Kind: %+v \n", mydata.Kind())
fmt.Printf("Kind2: %+v \n", reflect.ValueOf(mydata.Interface()).Kind())
}
Go Play: http://play.golang.org/p/TG4SzrtTf0