A struct to inherit the methods of another struct, here the current Person struct
type Person struct{
name string
age int
}
func (p Person) GetName()string{
return p.name
}
func (p Person) GetAge()int{
return p.age
}
A new struct
type called Student
which has all the properties and methods of Person
.
type Student struct{
Person
studentId int
}
func (s Student) GetStudentID() int {
return s.studentId
}
the type Person
inside the struct definition of type Student
, without specifying a field name. This will effectively make the Student
type inherit all the exported methods and fields of the Person struct
type.
s := Student{}
//This code is valid, because the method GetAge() belongs to the embedded type 'Person':
s.GetAge()
s.GetName()