Created
July 31, 2025 06:13
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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