Posted on 16th September 2024|139 views
Does Golang support struct inheritance?
Posted on 16th September 2024| views
Yes, as Golang does not support classes inheritance takes place through struct embedding.e We cannot directly extend structs, so we have to use the concept called composition where the struct is utilized to form the other objects. So, we can say there is no concept called inheritance in Golang. Look at this example
package main
import ("fmt")
type Action struct{
Universe string
}
func (action action) ActionUniverse() string {
return action.Universe
}
type Rampage struct{
Action
}
type DC struct{
Action
func main() {
a1:= Rampage{
Action{
Universe: "MCU",
},
}
fmt.Println("Universe is:", a1.ActionUniverse())
a2 := DC{
Action{
Universe : "DC",
},
}
fmt.Println("Universe is:", a2.ActionUniverse())
}