This is a creation in Article, where the information may have evolved or changed.
Inherited
Package Mainimport ("FMT") type people struct {name string age int weight Int}type Student struct {P Eople Specialty string}//Define Sayhi method on people Structfunc (P people) Sayhi () {fmt. PRINTLN (1)}func main () {p: = people{"Syy", 1, 2} s: = student{people{"Syy", 1, 2}, "Seecialty"} p.sayhi () S.S Ayhi ()}
The main function of the following method is to define a Sayhi method on the people structure.
Define Sayhi method on people Structfunc (P people) Sayhi () {fmt. PRINTLN (1)}
The people structure is defined, but student also has this method. Or it can be said that "people can say hello, students can certainly say hello."
So the Sayhi method of Strudent is inherited from people.
Replication
With the code above, we know the inheritance, and then we add the following code to the code.
Define Sayhi method on Student structfunc (S Student) Sayhi () {fmt. PRINTLN (2)}
The purpose of this code is to add a Sayhi method to the student structure.
By running, you can see that the output is 2 instead of the original 1
Therefore, the method of people Sayhi was sayhi by student method.