On Fri, 24 Jan 2014 03:47:24 -0800 (PST)
cvie...@gmail.com wrote:
> I was trying to define a new struct type with an anonymous map field.
> Something like this:
>
> type myMap map[string]string
> type myStruct struct {
> myMap
> }
>
> As expected I can create a map of type myMap and give values:
> r := make(myMap)
^^^ This sets r to the value of type myMap.
> r["hola"] = "adios"
> fmt.Println(r)
> // Prints: map[hola:adios]
>
> However, I tried creating a new variable of type myStruct and give
> values in the same fashion but doesn't work:
> r := &myStruct{make(myMap)}
^^^ This sets r to the value of type *myMap, that is, a pointer to
an instance of myMap.
> r["hola"] = "adios"
> fmt.Println(r)
> // Prints: invalid operation: r["hola"] (index of type *myStruct)
Because indexing operation does not work on pointers transparently --
only the dot "." does. Try to do
(*r)["hola"] = "adios"
instead.