SoFunction
Updated on 2025-03-04

The use of the basic interface of golang

An interface is a collection of one or more method signatures, defined as follows

type Interface_Name interface { method_a() string method_b() int .... } 

As long as a certain type has all method signatures of the interface, even if the interface is implemented, there is no need to display the declaration that the interface is implemented. This is called structural Typing

package main
import "fmt"

type USB interface { //Define an interface: a collection of methods  Name() string //Name method, return string  Connect()   //Connect method}

type PhoneConnecter struct { //Define a structure  name string //The structure contains a field}

func (pc PhoneConnecter) Name() string { //Define a method for the structure and bind the interface  return  //This method is named as a field within the interface} //Return to the structure field
func (pc PhoneConnecter) Connect() { //Define another method of the structure and bind another method of the interface  ("Connect:", )
}
func main() {
  var a USB              //Define a variable as USB interface type  a = PhoneConnecter{"PhoneConnecter"} //Instantiate a structure and assign it to the variable (interface) USB  ()             //The 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 structures.

package main
import "fmt"

type USB interface { //Define an interface: a collection of methods  Name() string //Name method, return string  Connecter   //Embed into the Connecter interface, you can use the Connecter method}

type Connecter interface { //Define another interface Connecter  Connect() //Contains an interface method: Connect}
type PhoneConnecter struct { //Define a structure  name string //The structure contains a field}

func (pc PhoneConnecter) Name() string { //Define a method for the structure and bind the interface  return  //This method is named as a field within the interface} //Return to the structure field
func (pc PhoneConnecter) Connect() { //Define another method of the structure and bind another method of the interface  ("Connect:", )
}
func main() {
  var a USB              //Define a variable as USB interface type  a = PhoneConnecter{"PhoneConnecter"} //Instantiate a structure and assign it to the variable (interface) USB  ()
  Disconnect(a) //The interface calls its Connect method, which is also a structural method}

The interface is just a method declaration, no implementation calls, no fields, and can only be accessed through the bound type method.

package main
import "fmt"

type USB interface { //Define an interface: a collection of methods  Name() string //Name method, return string  Connecter   //Embed into the Connecter interface, you can use the Connecter method}

type Connecter interface { //Define another interface Connecter  Connect() //Contains an interface method: Connect}
type PhoneConnecter struct { //Define a structure  name string //The structure contains a field}

func (pc PhoneConnecter) Name() string { //Define a method for the structure and bind the interface  return  //This method is named as a field within the interface} //Return to the structure field
func (pc PhoneConnecter) Connect() { //Define another method of the structure and bind another method of the interface  ("Connected:", )
}
func main() {
  var a USB           //Define a variable as USB interface type  a = PhoneConnecter{"htc M10"} //Instantiate a structure and assign it to the variable (interface) USB  ()
  Disconnect(a) //The interface calls its Connect method, which is also a structural method}

func Disconnect(usb USB) {
  if pc, ok := usb.(PhoneConnecter); ok { //Passing in the structure to determine whether the assignment is successful    ("Disconnected:", )
    return
  }
  ("UNknown device.")
}

/*Output
 Connect: htc M10
 Disconnect:htc M10
 */

Go does not inherit like other languages, such as object in python represents metaclasses. All classes are inherited from object classes. Go also implements this definition through interfaces, because as long as a certain type implements the method of an interface, we say that this class implements this interface. Because type empty interface{} ---The empty interface has no method, it can be understood that all interfaces implement methods (inheritance) of the empty interface.

package main
import "fmt"

type USB interface { //Define an interface: a collection of methods  Name() string //Name method, return string  Connecter   //Embed into the Connecter interface, you can use the Connecter method}

type Connecter interface { //Define another interface Connecter  Connect() //Contains an interface method: Connect}
type PhoneConnecter struct { //Define a structure  name string //The structure contains a field}

func (pc PhoneConnecter) Name() string { //Define a method for the structure and bind the interface  return  //This method is named as a field within the interface} //Return to the structure field
func (pc PhoneConnecter) Connect() { //Define another method of the structure and bind another method of the interface  ("Connected:", )
}
func main() {
  var a USB           //Define a variable as USB interface type  a = PhoneConnecter{"htc M10"} //Instantiate a structure and assign it to the variable (interface) USB  ()
  Disconnect(a) //The interface calls its Connect method, which is also a structural method}

# func Disconnect(usb interface{}) { //Integrated empty interface can also be implemented# if pc, ok := usb.(PhoneConnecter); ok { //Pass in the structure to determine whether the assignment is successful#    ("Disconnected:", )
#    return
#  }
#  ("UNknown device.")
#  }

//Judge interface type through switch: type... switch usagefunc Disconnect(usb interface{}) { //Integrated empty interface can also be implementedswitch v := usb.(type) {
case PhoneConnecter:
  ("Disconnected:", )
default:
  ("UNknown device.")
}
}

/*Output
 Connect: htc M10
 Disconnect:htc M10
 */

Conversion between interfaces: Only subclass interfaces can be converted to parent interfaces, because the parent interface contains subclass interfaces, and subclass interfaces can call some interface methods of the parent interface.

Assigning an object to the interface is a copy, and the interface stores a pointer that only wants this copy, which means that the interface cannot modify the state and cannot obtain the pointer.

...
func main() {
pc := PhoneConnecter{"ipad book pro"} //Instantiate a structurevar a Connecter            //Define a as an interface variablea = Connecter(pc)           //Interface cast()

 = "Iphone 7"
()
...

/*Output
 = "ipad book pro" Connected: ipad book pro
  = "Iphone 7" Connected: ipad book pro
 */
}

The interface is equal to nil only if the type and object stored by the interface are nil

package main
import "fmt"

func main() {
  var a interface{}
  (a == nil)

  var p *int = nil
  a = p
  (a == nil)
}

/*Output
 true
 false
 */

  1. Interface calls will not perform automatic conversion of Receiver
  2. The interface also supports anonymous fields.
  3. Interfaces can also implement polymorphisms similar to those in OOP (object-oriented programming).
  4. Empty interfaces can be used as containers of any type of data

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.