Interface Definition:
Interface { Method1 (param_list) return_type Method2 (param_list) return_type ...}
Note:
1. Cannot include variables in interface
2. A type does not explicitly define the implementation of an interface, as long as it contains all the methods defined in the interface, this type implements the interface by default
3. Multiple types can implement the same interface
4. While a type implements an interface, it can also define its own method
5. A single type can implement multiple interfaces
Nested interface
Interface { bool bool interface { Lock () Interface { ReadWrite Lock Close ()}
So file will contain all the methods in ReadWrite and lock
Determine the type of interface
V: = VarI. ( T)//VarI must is an interface variable.
Instance
Package Lemontype ShaperInterface{area () Float32}type Squarestruct{Side float32}func (sq*Square) Area () float32 {returnSq. Side *Sq. Side}//Use in main func.func showinterfaceusage () {sq1:=New(Lemon. Square) sq1. Side=5 varareaintf Lemon. Shaper areaintf=sq1ifV, OK: = areaintf. (*Lemon. Square); OK {fmt. Printf ("The square has an area :%f\n", V.area ())}}
Note: The type passed in the definition of the method is a pointer, and a pointer is also passed when judging.
Switch areaintf. (type) { case *Lemon. Square: FMT. Printf ("TheSquare area is%f\n", Areaintf.area ()) Default : fmt. Printf ("Thedefault type") }
can also be directly through the method interface. (type) to obtain the type of the interface
Determines whether the type implements the interface
... Cond
[Go language] learn go--interfaces from Docker source