Posted on 19th April 2024
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())
}
STILL GOT QUERIES?