Inheritance in GoLang Last Updated : 22 Jun, 2020 Comments Improve Suggest changes Like Article Like Report Inheritance means inheriting the properties of the superclass into the base class and is one of the most important concepts in Object-Oriented Programming. Since Golang does not support classes, so inheritance takes place through struct embedding. We cannot directly extend structs but rather use a concept called composition where the struct is used to form other objects. So, you can say there is No Inheritance Concept in Golang. In composition, base structs can be embedded into a child struct and the methods of the base struct can be directly called on the child struct as shown in the following example. Example 1: C // Golang program to illustrate the // concept of inheritance package main import ( "fmt" ) // declaring a struct type Comic struct{ // declaring struct variable Universe string } // function to return the // universe of the comic func (comic Comic) ComicUniverse() string { // returns comic universe return comic.Universe } // declaring a struct type Marvel struct{ // anonymous field, // this is composition where // the struct is embedded Comic } // declaring a struct type DC struct{ // anonymous field Comic } // main function func main() { // creating an instance c1 := Marvel{ // child struct can directly // access base struct variables Comic{ Universe: "MCU", }, } // child struct can directly // access base struct methods // printing base method using child fmt.Println("Universe is:", c1.ComicUniverse()) c2 := DC{ Comic{ Universe : "DC", }, } // printing base method using child fmt.Println("Universe is:", c2.ComicUniverse()) } Output: Universe is: MCU Universe is: DC Multiple inheritances take place when the child struct is able to access multiple properties, fields, and methods of more than one base struct. Here the child struct embeds all the base structs as shown through the following code: Example 2: C // Golang program to illustrate the // concept of multiple inheritances package main import ( "fmt" ) // declaring first // base struct type first struct{ // declaring struct variable base_one string } // declaring second // base struct type second struct{ // declaring struct variable base_two string } // function to return // first struct variable func (f first) printBase1() string{ // returns a string // of first struct return f.base_one } // function to return // second struct variable func (s second) printBase2() string{ // returns a string // of first struct return s.base_two } // child struct which // embeds both base structs type child struct{ // anonymous fields, // struct embedding // of multiple structs first second } // main function func main() { // declaring an instance // of child struct c1 := child{ // child struct can directly // access base struct variables first{ base_one: "In base struct 1.", }, second{ base_two: "\nIn base struct 2.\n", }, } // child struct can directly // access base struct methods // printing base method // using instance of child struct fmt.Println(c1.printBase1()) fmt.Println(c1.printBase2()) } Output: In base struct 1. In base struct 2. Comment More infoAdvertise with us Next Article Inheritance in GoLang vanigupta20024 Follow Improve Article Tags : Programming Language Go Language Golang-OOPs Similar Reads Interfaces in Golang In Go, an interface is a type that lists methods without providing their code. You canât create an instance of an interface directly, but you can make a variable of the interface type to store any value that has the needed methods.Exampletype Shape interface { Area() float64 Perimeter() float64}In t 3 min read Slices in Golang Slices in Go are a flexible and efficient way to represent arrays, and they are often used in place of arrays because of their dynamic size and added features. A slice is a reference to a portion of an array. It's a data structure that describes a portion of an array by specifying the starting index 14 min read Multiple Interfaces in Golang In Go language, the interface is a collection of method signatures and it is also a type means you can create a variable of an interface type. In Go language, you are allowed to create multiple interfaces in your program with the help of the given syntax: type interface_name interface{ // Method sig 4 min read Type Switches in GoLang A switch is a multi-way branch statement used in place of multiple if-else statements but can also be used to find out the dynamic type of an interface variable. A type switch is a construct that performs multiple type assertions to determine the type of variable (rather than values) and runs the fi 3 min read Embedding Interfaces in Golang In Go language, the interface is a collection of method signatures and it is also a type means you can create a variable of an interface type. As we know that the Go language does not support inheritance, but the Go interface fully supports embedding. In embedding, an interface can embed other inter 6 min read Packages in Golang Packages are the most powerful part of the Go language. The purpose of a package is to design and maintain a large number of programs by grouping related features together into single units so that they can be easy to maintain and understand and independent of the other package programs. This modula 5 min read Templates in GoLang Template in Golang is a robust feature to create dynamic content or show customized output to the user. Golang has two packages with templates: text/template html/template There are mainly 3 parts of a template which are as follows: 1. Actions They are data evaluations, control structures like loops 4 min read Parsing Time in Golang Parsing time is to convert our time to Golang time object so that we can extract information such as date, month, etc from it easily. We can parse any time by using time.Parse function which takes our time string and format in which our string is written as input and if there is no error in our form 2 min read Nested Structure in Golang A structure or struct in Golang is a user-defined type, which allows us to create a group of elements of different types into a single unit. Any real-world entity which has some set of properties or fields can be represented as a struct. Go language allows nested structure. A structure which is the 3 min read Pointer to a Struct in Golang In Go, structs are used to create custom data types that group different fields together. When working with structs, using pointers can be especially beneficial for managing memory efficiently and for avoiding unnecessary copying of data. A pointer to a struct allows you to directly reference and mo 3 min read Like