How do I access a map keyword using reflection?
I have a null key card:
mapp := map[interface{}]interface{}{
nil: "a",
}
Accessing it nil key directly works:
fmt.Println("key[nil]:", mapp[nil])
But with reflection this is not the case - how to do it?
rmapp := reflect.ValueOf(mapp)
rkey := reflect.ValueOf(interface{}(nil))
rval := rmapp.MapIndex(rmapp.MapIndex(rkey))
fmt.Println("key[nil]:", rval)
The broken code is here:
https://play.golang.org/p/6TKN_tDNgV
+3
source to share
2 answers