nested struct initialization literals

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.

Leave a Comment

tech