struct
keyword asstruct {field type}
name
and age
are called fields of struct.type
keyword for reuse.person
is a user defined struct
type. p1
and p2
variables are both of type person
..
This can be used to read as well as write to struct fields.==.
superman.human.name
superman.name
instead of superman.human.name
in above code? Go supports this by providing type embedding. An inner struct that is stored within an outer struct using an anonymous field is said to be embedded within the outer struct.human
type inside superHero
type. There is no named field inside superHero
to which we are assigning human
type. Outside struct literal, we can access fields of human
type directly as if these fields exists on superHero
. This is called type promotion.