FAQ
Is is possible to have an anonymous field in a struct, which is an array?

type Foo struct {
   []string
}

fails with "syntax error: unexpected ["

Whereas

type Foo struct {
   Thingy []string
}

works fine.

Thanks in advance.

Pete

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

  • Alexander Neumann at Dec 4, 2013 at 7:47 pm
    The embedded slice must be a named type.

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

    Am Mittwoch, 4. Dezember 2013 20:37:04 UTC+1 schrieb Peter Ward:
    Is is possible to have an anonymous field in a struct, which is an array?

    type Foo struct {
    []string
    }

    fails with "syntax error: unexpected ["

    Whereas

    type Foo struct {
    Thingy []string
    }

    works fine.

    Thanks in advance.

    Pete
    --
    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.
  • Kevin Gillette at Dec 4, 2013 at 7:58 pm
    The only difference between `struct { T }` and `struct { T T }` is that in
    the first case, T's fields and methods are embedded in the top level struct
    as a purely compile-time, syntactic convenience. There is no difference in
    memory layout, speed of execution, etc. Since unnamed types such as
    []string (which is a slice, not an array) cannot have methods, then there
    would be little or no potential benefit to embedding them, even if it were
    allowed.
    On Wednesday, December 4, 2013 12:37:04 PM UTC-7, Peter Ward wrote:

    Is is possible to have an anonymous field in a struct, which is an array?

    type Foo struct {
    []string
    }

    fails with "syntax error: unexpected ["

    Whereas

    type Foo struct {
    Thingy []string
    }

    works fine.

    Thanks in advance.

    Pete
    --
    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
postedDec 4, '13 at 7:37p
activeDec 4, '13 at 7:58p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase