In our previous post, we discussed the anonymous structs and fields, but there is one feature we have not discussed: Embedded structs and Promoted fields. This post will discuss them in detail.
Embedded structs
To understand Embedded structs, you should learn about the anonymous structs and fields.
Struct fields with no name and just the data type are anonymous fields.
type Company struct {
department string
Employee // Anonymous field
}
An inner struct that is stored within an outer struct using an anonymous field is said to be embedded within the outer struct. We can use an anonymous field to make our nested struct more easily accessible.
So, Employee will be treated as an embedded struct. We can directly access an Embedded struct using the struct name itself.
Let’s look at an example where we will access the Employee struct using the struct name.
package main
import (
"fmt"
)
type Employee struct {
firstName string
lastName string
age int
height float64
}
type Company struct {
department string
Employee // Anonymous field
}
func main() {
employee1 := Employee{"codekru", "website", 22, 167.12} // defining the employee struct
company1 := Company{"CS", employee1} // defining the company struct
// accessing the Employee anonymous field
// using the type itself
fmt.Println("Employee Details:", company1.Employee)
fmt.Println("Employee Age:", company1.Employee.age)
}
Output –
Employee Details: {codekru website 22 167.12}
Employee Age: 22
Promoted fields
Fields that belong to an embedded struct can be accessed directly through the outer struct variable. So, we can say that the fields of the embedded struct are promoted to the outer struct, and thus they are called the promoted fields.
Let’s take the same example of the Company and the Employee struct, where the Employee will be treated as an embedded struct.
package main
import (
"fmt"
)
type Employee struct {
firstName string
lastName string
age int
height float64
}
type Company struct {
department string
Employee // Anonymous field
}
func main() {
employee1 := Employee{"codekru", "website", 22, 167.12} // defining the employee struct
company1 := Company{"CS", employee1} // defining the company struct
// accessing the Employee fields directly from the company struct
fmt.Println("Employee age:", company1.age)
fmt.Println("Employee firstName:", company1.firstName)
fmt.Println("Employee height:", company1.height)
}
Output –
Employee age: 22
Employee firstName: codekru
Employee height: 167.12
Here, we have directly accessed the fields of the Employee from the Company struct, as if they are part of the Company struct itself.
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.
I’ⅾ like to thank you for the effoгts you have рut in writing this blog.
I am hoping to checқ out the same high-grade blog
posts by you in the future aѕ ᴡell. In fact, уour creative writіng abilities
has inspired me to get my very own site now 😉