struct interface can realize object-oriented inheritance, encapsulation, and polymorphism
Demonstration of inheritance:
The Tsh type inherits the People type and uses the methods of the People type
Polymorphic demonstration
Tsh type implements the interface Student and implements the methods defined by the interface
Full code:
package main import "fmt" //Parent type type People struct { } func (p *People) echo() { fmt.Println("taoshihan") } //Interface type Student interface { Do() } //Subtype, implements the interface and inherits the parent type type Tsh struct { People } func (t Tsh) Do() { fmt.Println("taoshihan do") } func main() { //Demo of inheritance t := Tsh{People{}} t.echo() //Polymorphic demonstration var student Student student = t student.Do() }