FAQ
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

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Konstantin Khomoutov at Jan 24, 2014 at 1:37 pm

    On Fri, 24 Jan 2014 03:47:24 -0800 (PST) cviedmai@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.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Harald Weidner at Jan 24, 2014 at 1:42 pm
    Hello,

    cviedmai@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

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gustavo Niemeyer at Jan 24, 2014 at 1:52 pm

    On Fri, Jan 24, 2014 at 9:47 AM, 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

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Cristobal Viedma at Jan 24, 2014 at 4:59 pm
    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.


    On Fri, Jan 24, 2014 at 2:51 PM, Gustavo Niemeyer wrote:
    On Fri, Jan 24, 2014 at 9:47 AM, 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
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 24, '14 at 1:29p
activeJan 24, '14 at 4:59p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase