Recently Buckle School online blockchain training courses, including from the beginner to proficient, go language and other course content, there are many students reflect a problem for the use of interface interface do not know how to solve, the teacher according to the needs of students to write an article, now share to everyone, below we look at it.
An interface is a collection of one or more method signature names, defined in the following way
Typeinterface_nameinterface{method_a () stringmethod_b () int ....}
As long as a type has all the method signatures of that interface, even if the interface is implemented, it is not necessary to show that the declaration implements that interface, which is called structuraltyping
Packagemain
Import "FMT"
typeusbinterface{//defines an interface: a collection of methods
Name () String//name method, returns a string
Connect ()//connect method
}
typephoneconnecterstruct{//Defining a structure
The namestring//structure consists of a field
}
Func (Pcphoneconnecter) Name () string{//defines a method for the struct, which binds the interface
returnpc.name//the method is named as a field within the interface
}//return struct field
Func (Pcphoneconnecter) Connect () {//define struct body Another method, bind with another method of the interface
Fmt. Println ("Connect:", Pc.name)
}
Funcmain () {
varausb//defines a variable as a USB interface type
a=phoneconnecter{"Phoneconnecter"}//instantiate a struct and assign a value to the variable (interface) USB
The A.connect ()//interface calls its Connect method, which is also a structural method
}
/* Output
Connect:phoneconnecter
*/
Interfaces can be embedded in other interfaces as anonymous, or embedded in a structure
Packagemain
Import "FMT"
typeusbinterface{//defines an interface: a collection of methods
Name () String//name method, returns a string
connecter//Embedded Connecter interface, you can use the Connecter method
}
typeconnecterinterface{//define another interface connecter
Connect ()//contains an interface method: Connect
}
typephoneconnecterstruct{//Defining a structure
The namestring//structure consists of a field
}
Func (Pcphoneconnecter) Name () string{//defines a method for the struct, which binds the interface
returnpc.name//the method is named as a field within the interface
}//return struct field
Func (Pcphoneconnecter) Connect () {//define struct body Another method, bind with another method of the interface
Fmt. Println ("Connect:", Pc.name)
}
Funcmain () {
varausb//defines a variable as a USB interface type
a=phoneconnecter{"Phoneconnecter"}//instantiate a struct and assign a value to the variable (interface) USB
A.connect ()
Disconnect (a)//interface calls its Connect method and is also a structural method
}
Interfaces are just method declarations, no implementation calls, no fields, only access through bound type methods
Packagemain
Import "FMT"
typeusbinterface{//defines an interface: a collection of methods
Name () String//name method, returns a string
connecter//Embedded Connecter interface, you can use the Connecter method
}
typeconnecterinterface{//define another interface connecter
Connect ()//contains an interface method: Connect
}
typephoneconnecterstruct{//Defining a structure
The namestring//structure consists of a field
}
Func (Pcphoneconnecter) Name () string{//defines a method for the struct, which binds the interface
returnpc.name//the method is named as a field within the interface
}//return struct field
Func (Pcphoneconnecter) Connect () {//define struct body Another method, bind with another method of the interface
Fmt. Println ("Connected:", Pc.name)
}
Funcmain () {
varausb//defines a variable as a USB interface type
a=phoneconnecter{"HtcM10"}//instantiate a struct and assign a value to the variable (interface) USB
A.connect ()
Disconnect (a)//interface calls its Connect method and is also a structural method
}
Funcdisconnect (USBUSB) {
Ifpc,ok:=usb. (phoneconnecter); ok{//incoming structure to determine whether the assignment was successful
Fmt. Println ("Disconnected:", pc.name)
Return
}
Fmt. Println ("Unknowndevice.")
}
/* Output
Connect:htcm10
Disconnect:htcm10
*/
Go does not inherit like other languages, such as the object in Python represents a meta-class, all classes inherit from the object class, and go implements this definition similarly through interfaces, because as long as a type implements the method of an interface, we say that this class implements the interface. Because typeemptyinterface{}---NULL interface has no method, it can be understood that all interfaces implement a method (inheritance) of NULL interfaces.
Packagemain
Import "FMT"
typeusbinterface{//defines an interface: a collection of methods
Name () String//name method, returns a string
connecter//Embedded Connecter interface, you can use the Connecter method
}
typeconnecterinterface{//define another interface connecter
Connect ()//contains an interface method: Connect
}
typephoneconnecterstruct{//Defining a structure
The namestring//structure consists of a field
}
Func (Pcphoneconnecter) Name () string{//defines a method for the struct, which binds the interface
returnpc.name//the method is named as a field within the interface
}//return struct field
Func (Pcphoneconnecter) Connect () {//define struct body Another method, bind with another method of the interface
Fmt. Println ("Connected:", Pc.name)
}
Funcmain () {
varausb//defines a variable as a USB interface type
a=phoneconnecter{"HtcM10"}//instantiate a struct and assign a value to the variable (interface) USB
A.connect ()
Disconnect (a)//interface calls its Connect method and is also a structural method
}
#funcDisconnect (usbinterface{}) {//Integrated null interface, can also be implemented
#ifpc, Ok:=usb. (phoneconnecter); ok{//incoming structure to determine whether the assignment was successful
#fmt. Println ("Disconnected:", pc.name)
#return
#}
#fmt. Println ("Unknowndevice.")
#}
To determine the interface type by switch: Type...switch usage
Funcdisconnect (usbinterface{}) {//Integrated null interface, can also be implemented
Switchv:=usb. (type) {
Casephoneconnecter:
Fmt. Println ("Disconnected:", v.name)
Default
Fmt. Println ("Unknowndevice.")
}
}
/* Output
Connect:htcm10
Disconnect:htcm10
*/
Conversion between interfaces: Only the subclass interface can be converted to the parent class interface, because the parent class interface contains the subclass interface, the subclass interface can call the parent class interface part of the interface method
Assigning an object to an interface is a copy, and the interface internally stores a pointer to that replica, which means that the interface cannot modify the state or get the pointer.
...
Funcmain () {
pc:=phoneconnecter{"Ipadbookpro"}//instantiate a structure
varaconnecter//defines a as an interface variable
A=connecter (PC)//Interface Casting
A.connect ()
Pc.name= "Iphone7"
A.connect ()
...
/* Output
Pc.name= "Ipadbookpro" Connected:ipadbookpro
Pc.name= "Iphone7" Connected:ipadbookpro
*/
}
The interface is equal to nil only if the interface stores both the type and the object are nil
Packagemain
Import "FMT"
Funcmain () {
varainterface{}
Fmt. Println (A==nil)
Varp*int=nil
A=p
Fmt. Println (A==nil)
}
/* Output
True
False
*/
Interface calls do not automatically convert receiver
Interface also supports anonymous field methods
Interfaces can also implement polymorphism in OOP (object-oriented programming) like
An empty interface can be used as a container for any type of data
The above is the whole content of this article, I hope that everyone's study has helped.