logo-MindMajix
PostGoLangGoLang
v
victoria mamidi

Posted on 25th July 2024|139 views

0
votes

Golang Struct Inheritance

Does Golang support struct inheritance?

1 answers
Answers
P
Tom 234

Posted on 25th July 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())

}

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo

  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos
Name
Course *
Email *
Phone Number