This is a creation in Article, where the information may have evolved or changed.
The following code is validated by the go1.5
Package Main
Import (
"FMT"
)
Type person struct {
Name string
Age int
Tel string
}
Type Student struct {
person//has another field
School string
}
Func (P *person) Print () {
Fmt. Printf ("print\n")
P.hello ()//Hello to Person
}
Defines a method for passing values on person
Func (P *person) Hello () {
P.tel = "186"
Fmt. Printf ("Person My name was%s, and my tel number is%s\n", P.name, P.tel)
}
Polymorphic
Func (P *student) Hello () {
P.tel = "0117"
Fmt. Printf ("Student My name is%s, and my tel number is%s\n", P.name, P.tel)
}
Func Main () {
Anna: = new (Student)
Anna. Person.name = "Jim"
Anna.tel = "12345678"
Anna. Hello () //student My name is Jim, and my tel number is 0117
Anna. Person.hello () //person My name is Jim, and my tel number is 186
Anna. Print () //print
Here in C + + it calls student's Hello interface
}