March 2019
Beginner to intermediate
324 pages
7h 17m
English
But what if you would like a struct to inherit the methods of another struct? The closest feature that the Go language offers to the concept of inheritance is known as type embedding. This feature is best explained through an example. Let's go back to the Person struct type:
type Person struct{ name string age int}func (p Person) GetName()string{ return p.name}func (p Person) GetAge()int{ return p.age}
Now, let's say that we would like to create a new struct type called Student, which has all the properties and methods of Person, plus some more:
type Student struct{ Person studentId int}func (s Student) GetStudentID()int{ return s.studentId}
Notice that in the preceding code, we included the type Person inside the struct definition ...
Read now
Unlock full access