41

How can I do this:

type A struct {
    MemberA string
}

type B struct {
    A A
    MemberB string
}

...

b := B {
    MemberA: "test1",
    MemberB: "test2",
}
fmt.Printf("%+v\n", b)

Compiling that gives me: "unknown B field 'MemberA' in struct literal"

How can I initialize MemberA (from the "parent" struct) when I provide literal struct member values like this?

golopot
  • 10,726
  • 6
  • 37
  • 51
Brad Peabody
  • 10,917
  • 9
  • 44
  • 63

2 Answers2

45

While initialization the anonymous struct is only known under its type name (in your case A). The members and functions associated with the struct are only exported to the outside after the instance exists.

You have to supply a valid instance of A to initialize MemberA:

b := B {
    A: A{MemberA: "test1"},
    MemberB: "test2",
}

The compiler error

unknown B field 'MemberA' in struct literal

says exactly that: there's no MemberA as it is still in A and not in B. In fact, B will never have MemberA, it will always remain in A. Being able to access MemberA on an instance of B is only syntactic sugar.

nemo
  • 55,207
  • 13
  • 135
  • 135
  • Perfect - yeah I tried `B { A{MemberA: "test1"}, MemberB: "test2" }` but that doesn't work either - looks like you have to treat the nested struct like a member as you have above. Thanks! – Brad Peabody Oct 11 '13 at 19:43
-4

The problem is with declaring the struct A in B. Please specify the name along with declaration, then it works.

package main

import "fmt"

type A struct {
    MemberA string
}

type B struct {
    MemA    A
    MemberB string
}

func main() {
    b := B{MemA: A{MemberA: "test1"}, MemberB: "test2"}
    fmt.Println(b.MemberB)
}
Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
Anonymous
  • 1
  • 1
  • 2
    _Declaring_ a field and _embedding_ a type are 2 different things. If you specify a name, that is declaring. When you omit the name, that is embedding. You are not answering the question, your answer is an alternative proposal. – icza Oct 13 '15 at 09:38