FAQ
Hi,

I've searched all over and can't find the answer to this.

I have a struct I would like to extend a map. Based on what I've read it
should look like :


type MyMap struct {
*map[string]interface{}
SomeField string
}

or possibly this:

type MyMap map[string]interface{} {
SomeField string
}


Neither work. Is there a way to do this?

thanks much,
Dustin

--

Search Discussions

  • Archos at Nov 28, 2012 at 11:27 pm
    There are several ways:

    http://play.golang.org/p/sZqVHPGdNl

    El miércoles, 28 de noviembre de 2012 23:13:37 UTC, Dustin Norlander
    escribió:
    Hi,

    I've searched all over and can't find the answer to this.

    I have a struct I would like to extend a map. Based on what I've read it
    should look like :


    type MyMap struct {
    *map[string]interface{}
    SomeField string
    }

    or possibly this:

    type MyMap map[string]interface{} {
    SomeField string
    }


    Neither work. Is there a way to do this?

    thanks much,
    Dustin
    --
  • Bryanturley at Nov 28, 2012 at 11:32 pm

    On Wednesday, November 28, 2012 5:27:48 PM UTC-6, Archos wrote:
    There are several ways:

    http://play.golang.org/p/sZqVHPGdNl
    meh, except that isn't really extending it

    http://play.golang.org/p/ozwnBJm26p <--- he probably wants the doesn't
    work example

    --
  • Archos at Nov 28, 2012 at 11:48 pm
    El miércoles, 28 de noviembre de 2012 23:32:53 UTC, bryanturley escribió:
    On Wednesday, November 28, 2012 5:27:48 PM UTC-6, Archos wrote:

    There are several ways:

    http://play.golang.org/p/sZqVHPGdNl
    meh, except that isn't really extending it

    http://play.golang.org/p/ozwnBJm26p <--- he probably wants the doesn't
    work example

    The same for your example:
    http://play.golang.org/p/Bl2O3FbNoV

    --
  • Dustin Norlander at Nov 28, 2012 at 11:46 pm
    Thanks guys. Looks like what I wanted just isn't possible. good to know.

    -Dustin


    On Wednesday, November 28, 2012 3:41:12 PM UTC-8, Archos wrote:



    El miércoles, 28 de noviembre de 2012 23:32:53 UTC, bryanturley escribió:
    On Wednesday, November 28, 2012 5:27:48 PM UTC-6, Archos wrote:

    There are several ways:

    http://play.golang.org/p/sZqVHPGdNl
    meh, except that isn't really extending it

    http://play.golang.org/p/ozwnBJm26p <--- he probably wants the doesn't
    work example

    The same for your example:
    http://play.golang.org/p/Bl2O3FbNoV
    --
  • Bryanturley at Nov 28, 2012 at 11:57 pm

    On Wednesday, November 28, 2012 5:41:12 PM UTC-6, Archos wrote:


    El miércoles, 28 de noviembre de 2012 23:32:53 UTC, bryanturley escribió:
    On Wednesday, November 28, 2012 5:27:48 PM UTC-6, Archos wrote:

    There are several ways:

    http://play.golang.org/p/sZqVHPGdNl
    meh, except that isn't really extending it

    http://play.golang.org/p/ozwnBJm26p <--- he probably wants the doesn't
    work example

    The same for your example:
    http://play.golang.org/p/Bl2O3FbNoV
    Indeed, that is why I defined Get() and Set() and showed and example of
    their use.

    --
  • Archos at Nov 29, 2012 at 6:42 am
    El miércoles, 28 de noviembre de 2012 23:57:45 UTC, bryanturley escribió:

    On Wednesday, November 28, 2012 5:41:12 PM UTC-6, Archos wrote:



    El miércoles, 28 de noviembre de 2012 23:32:53 UTC, bryanturley escribió:
    On Wednesday, November 28, 2012 5:27:48 PM UTC-6, Archos wrote:

    There are several ways:

    http://play.golang.org/p/sZqVHPGdNl
    meh, except that isn't really extending it

    http://play.golang.org/p/ozwnBJm26p <--- he probably wants the
    doesn't work example

    The same for your example:
    http://play.golang.org/p/Bl2O3FbNoV
    Indeed, that is why I defined Get() and Set() and showed and example of
    their use.
    Of course, and I'm sure that the OP would know add a methods to a structs
    already implemented like in mi examples.

    --
  • Bryanturley at Nov 28, 2012 at 11:31 pm
    You can't extend map, you can use it in a struct and use the struct as a
    non built-in map though.

    http://play.golang.org/p/Rj-JrHKdAf

    don't use interface {} if you can use something specific


    --
  • Kevin Gillette at Nov 29, 2012 at 6:39 pm
    What you're suggesting is embedding an unnamed type. Since brace, bracket,
    and asterisk characters are not valid identifier characters, you couldn't
    access the underlying field anyway --

    m := MyMap{}
    m.*map[string]interface{}["something"]

    That would be a syntax error.

    You can embed any unnamed type:

    type underlyingmap map[string]interface{}

    type MyMap struct {
    underlyingmap
    SomeField string
    }

    m := MyMap{}
    m.underlyingmap["something"]

    The above should work.
    On Wednesday, November 28, 2012 4:13:37 PM UTC-7, Dustin Norlander wrote:

    Hi,

    I've searched all over and can't find the answer to this.

    I have a struct I would like to extend a map. Based on what I've read it
    should look like :


    type MyMap struct {
    *map[string]interface{}
    SomeField string
    }

    or possibly this:

    type MyMap map[string]interface{} {
    SomeField string
    }


    Neither work. Is there a way to do this?

    thanks much,
    Dustin
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 28, '12 at 11:13p
activeNov 29, '12 at 6:39p
posts9
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase