Code with Go
Ask or search…

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())


co={num: 1, str: some name}
also num: 1
describe: base with num=1
describer: base with num=1
  1. 1.
    The base struct has a single field, num, and a method called describe that returns a string describing the base struct.
  2. 2.
    The container struct embeds the base struct, effectively inheriting its fields and methods. It also has an additional field, str.
  3. 3.
    In the main function:
    • An instance co of the container struct is created with field values.
    • Field values of the container struct are printed, and the embedded base struct field (num) is accessed directly.
    • The describe method is called on the container struct, which internally calls the describe method of the embedded base struct.
    • An interface named describer is defined with a describe method.
    • A variable d of type describer is created and assigned the container struct.
    • The describe method is called on the describer interface, demonstrating method overriding. The overridden method in the container struct is called.
Last modified 1mo ago