We have 3 types:
type A struct {
B
C
}
type B struct {
x int
y string
}
type C struct {
z string
}
because of how fields and methods of an anonymous field are promoted, we can access fields of the anonymous field B
in A
like
var a A
a.x = 0
It is very obvious that type B
& C
embed in A
, so we expect A
to be equivalent to:
type D struct {
x int
y string
z string
}
What did you expect to see?
We expect that we can write composite literals of type A
like that:
a := A{x: 2}
What did you see instead?
This compile error:
unknown field 'x' in struct literal of type A
Our Question
Why isn't it possible to write composite literals for A
in that way, as it would for the type D
?