Struct Embedding
This Go program demonstrates the concept of composition in Go structs, where a struct includes another struct as a field. It also showcases method overriding through embedding and interface implementation. Let's go through each part of the code with inline comments and additional explanations:
package main
import "fmt"
// Define a struct named 'base' with a field 'num'
type base struct {
num int
}
// Method 'describe' for the 'base' struct, returns a string describing the base struct
func (b base) describe() string {
return fmt.Sprintf("base with num=%v", b.num)
}
// Define a struct named 'container' that embeds the 'base' struct and has an additional field 'str'
type container struct {
base // Embedding the 'base' struct
str string
}
func main() {
// Create an instance of the 'container' struct with field values
co := container{
base: base{
num: 1,
},
str: "some name",
}
// Print the values of the 'container' struct fields
fmt.Printf("co={num: %v, str: %v}\n", co.num, co.str)
// Access the 'num' field of the embedded 'base' struct directly
fmt.Println("also num:", co.base.num)
// Call the 'describe' method on the 'container' struct, which internally calls the 'describe' method of the embedded 'base' struct
fmt.Println("describe:", co.describe())
// Define an interface named 'describer' with a 'describe' method
type describer interface {
describe() string
}
// Create a variable 'd' of type 'describer' and assign the 'container' struct to it
var d describer = co
// Call the 'describe' method on the 'describer' interface, which calls the overridden method in the 'container' struct
fmt.Println("describer:", d.describe())
}Output
co={num: 1, str: some name}
also num: 1
describe: base with num=1
describer: base with num=1Explanation:
The
basestruct has a single field,num, and a method calleddescribethat returns a string describing the base struct.The
containerstruct embeds thebasestruct, effectively inheriting its fields and methods. It also has an additional field,str.In the
mainfunction:An instance
coof thecontainerstruct is created with field values.Field values of the
containerstruct are printed, and the embeddedbasestruct field (num) is accessed directly.The
describemethod is called on thecontainerstruct, which internally calls thedescribemethod of the embeddedbasestruct.An interface named
describeris defined with adescribemethod.A variable
dof typedescriberis created and assigned thecontainerstruct.The
describemethod is called on thedescriberinterface, demonstrating method overriding. The overridden method in thecontainerstruct is called.
Last updated
Was this helpful?