Skip to content

Instantly share code, notes, and snippets.

@Sedose
Created July 31, 2025 06:13
Show Gist options
  • Select an option

  • Save Sedose/ddbdf7fafffa1144e643b08656cf070b to your computer and use it in GitHub Desktop.

Select an option

Save Sedose/ddbdf7fafffa1144e643b08656cf070b to your computer and use it in GitHub Desktop.
This Go program demonstrates struct embedding aka automatic boilerplate free delegation. Try it out https://go.dev/play/p/0rauYusjgh8
package main
import "fmt"
type FifthLevelA struct{}
func (f FifthLevelA) ExecuteA() string {
return "executed in fifth level"
}
type FifthLevelB struct{}
func (f FifthLevelB) ExecuteB() string {
return "executed in fifth level"
}
type FourthLevel struct {
FifthLevelA
FifthLevelB
}
type ThirdLevel struct {
FourthLevel
}
type SecondLevel struct {
ThirdLevel
}
type FirstLevel struct {
SecondLevel
}
func main() {
top := FirstLevel{}
fmt.Println(top.ExecuteA()) // → "executed in fifth level"
fmt.Println(top.ExecuteB()) // → "executed in fifth level"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment