This is a creation in Article, where the information may have evolved or changed.
In the go language, as long as a type implements an interface containing a method, we can assume that the type implements the interface, the following is an example of a simple type and interface in the go language.
Package Mainimport ( "FMT")//define an interface to implement the Run method type Irun interface { run ()}//define an adult type of man struct {}// Define a child type a kid struct {}//implements the Run method for adults, and essentially implements the Irun interface func (M *man) run () {FMT) for adults . PRINTLN ("Adult Running")}//for the child to implement the Run method, the essence is to implement the child Irun interface func (k *kid) run () { fmt. PRINTLN ("Child Running")}func main () { var krun irun = new (Kid) Krun.run () var mrun irun = new (man) Mrun.run ()}
X---------------------running Results------------------x//
The kid's running.
The adults are running.