I have a new type derived from map such:
type mapp map[string]interface{}
with a small function on it
func (c mapp) Set() error {
// c is nil
c["a"] = "b"
return nil
}
type Setter interface {
Set() error
}
func main() {
var aa mapp
out := reflect.ValueOf(&aa)
s := out.Interface().(Setter)
s.Set()
}
This code works on a struct, why this code fails when it comes to a type of a map?
Here's a playground: https://play.golang.org/p/Z1LFqb6kF7
Many thanks,
Asaf.
Go maps (and slices) are created via make
. The equivalent function in reflect
is reflect.MakeMap
out := reflect.ValueOf(&aa).Elem()
out.Set(reflect.MakeMap(out.Type()))
s := out.Interface().(Setter)
s.Set()