Anonymous maps

577 views
Skip to first unread message

cvie...@gmail.com

unread,
Jan 24, 2014, 6:47:24 AM1/24/14
to golan...@googlegroups.com
Hi,

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)
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)}
r["hola"] = "adios"
fmt.Println(r)
// Prints: invalid operation: r["hola"] (index of type *myStruct)

I was wondering if there is any way to set/get data from the anonymous map using the [] syntax. Obviously I could not use an anonymous map and just refer to the attribute (or creating some setters/getters), but I think it would be cleaner if I could just do it this way.

Thanks!

Cristobal

Konstantin Khomoutov

unread,
Jan 24, 2014, 8:37:04 AM1/24/14
to cvie...@gmail.com, golan...@googlegroups.com
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.

Harald Weidner

unread,
Jan 24, 2014, 8:42:14 AM1/24/14
to golan...@googlegroups.com
Hello,

cvie...@gmail.com:

>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)}
>r["hola"] = "adios"
>fmt.Println(r)
>// Prints: invalid operation: r["hola"] (index of type *myStruct)

I am not sure if this is what you are looking for, but you can use

s.myMap["hola"] = "adios"

Harald

Gustavo Niemeyer

unread,
Jan 24, 2014, 8:51:57 AM1/24/14
to cvie...@gmail.com, golan...@googlegroups.com
On Fri, Jan 24, 2014 at 9:47 AM, <cvie...@gmail.com> wrote:
> I was wondering if there is any way to set/get data from the anonymous map
> using the [] syntax. Obviously I could not use an anonymous map and just
> refer to the attribute (or creating some setters/getters), but I think it
> would be cleaner if I could just do it this way.

It doesn't work. Structs cannot be indexed:

http://golang.org/ref/spec#Index_expressions


gustavo @ http://niemeyer.net

Cristobal Viedma

unread,
Jan 24, 2014, 11:58:57 AM1/24/14
to Gustavo Niemeyer, golan...@googlegroups.com
the (*r)["hola"] don't work.
@gustavo, yup I saw structs cannot be indexed, somehow I was hoping that "delegated" the indexing to the anonymous map, but I guess there is no such magic.

Reply all
Reply to author
Forward
0 new messages