FAQ
Hey everyone, I am trying to figure out the right syntax to initialize an
array of structs. For example, I have this struct

type ColorGroup struct {
     ID int
     Name string
     Colors [] struct {
         Name string
         Title string
     }
}

I want to do something like

     group := ColorGroup{
         ID: 1,
         Name: "notify",
         Colors: []struct {{"red", "bad"}, {"green","good"}}
     }

I know this doesn't work but I was wondering if you might be able to shed
some light on this.

thx!

Jian

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

  • Jimmy frasche at May 8, 2013 at 7:16 pm
    Slices ≠ arrays.

    If you don't name the struct you have to replicate it wherever you use
    it, so the line would be:

    Colors: []struct{Name, Title string} {
        {"red", "bad"},
        {"green", "good"},
    }

    It makes more sense in something like a table of tests where you'd
    have something like:

    var Colors = []struct{Name, Title string} {
        {"red", "bad"},
        {"green", "good"},
    }

    at the top level. Confer essentially any random test file in the
    standard library to see the above in action.

    --
    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.
  • Péter Szilágyi at May 8, 2013 at 7:18 pm
    It would be a bit wiser to remove the inner anonymous struct. Then you
    could simply: http://play.golang.org/p/pNU33mFTaf

    On Wed, May 8, 2013 at 9:01 PM, Jian Zhen wrote:

    Hey everyone, I am trying to figure out the right syntax to initialize an
    array of structs. For example, I have this struct

    type ColorGroup struct {
    ID int
    Name string
    Colors [] struct {
    Name string
    Title string
    }
    }

    I want to do something like

    group := ColorGroup{
    ID: 1,
    Name: "notify",
    Colors: []struct {{"red", "bad"}, {"green","good"}}
    }

    I know this doesn't work but I was wondering if you might be able to shed
    some light on this.

    thx!

    Jian

    --
    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.

    --
    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.
  • Jian Zhen at May 8, 2013 at 7:35 pm
    Thanks everyone! Looks like removing the anonymous struct is the way to Go as everyone suggested.
    On May 8, 2013, at 12:18 PM, Péter Szilágyi wrote:

    It would be a bit wiser to remove the inner anonymous struct. Then you could simply: http://play.golang.org/p/pNU33mFTaf


    On Wed, May 8, 2013 at 9:01 PM, Jian Zhen wrote:
    Hey everyone, I am trying to figure out the right syntax to initialize an array of structs. For example, I have this struct

    type ColorGroup struct {
    ID int
    Name string
    Colors [] struct {
    Name string
    Title string
    }
    }

    I want to do something like

    group := ColorGroup{
    ID: 1,
    Name: "notify",
    Colors: []struct {{"red", "bad"}, {"green","good"}}
    }

    I know this doesn't work but I was wondering if you might be able to shed some light on this.

    thx!

    Jian

    --
    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.

    --
    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.
  • Jimmy frasche at May 8, 2013 at 7:41 pm
    the way to Go
    This should be our version of pythonic: our idiomatic way to say idiomatic.

    --
    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.
  • Gordon Klaus at May 8, 2013 at 7:23 pm
    []struct is not a type. Colors: []struct{Name, Title string}{...} is what
    you are looking for. I would suggest just naming your color type so you
    don't have to spell it out every time.
    On Wednesday, May 8, 2013 3:01:31 PM UTC-4, Jian Zhen wrote:

    Hey everyone, I am trying to figure out the right syntax to initialize an
    array of structs. For example, I have this struct

    type ColorGroup struct {
    ID int
    Name string
    Colors [] struct {
    Name string
    Title string
    }
    }

    I want to do something like

    group := ColorGroup{
    ID: 1,
    Name: "notify",
    Colors: []struct {{"red", "bad"}, {"green","good"}}
    }

    I know this doesn't work but I was wondering if you might be able to shed
    some light on this.

    thx!

    Jian
    --
    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
postedMay 8, '13 at 7:08p
activeMay 8, '13 at 7:41p
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase