Go interface

In the Go programming language, an interface is an abstract reference type that defines the behavior (methods) of an object. It outlines the specifications but does not implement them itself; the concrete implementation details are left to individual objects. Here's how an interface is defined:

type InterfaceName interface {  
    MethodName1(paramList1) returnList1  
    MethodName2(paramList2) returnList2  
}

Conditions for Implementing an Interface

In Go, interfaces are implemented implicitly: If a type provides all the methods defined in an interface, it is automatically considered to have implemented that interface. In other words, an interface is a collection of methods that need to be implemented.

type PayMethod interface {
   pay()
   currency()
}

type Staff interface {
   getEmployeeInfo()
}

type Cregend struct {
   Name string
}
type Synfun struct {
   Name string
}

// Cregend type implements the PayMethod interface
func (this Cregend) pay() {
   fmt.Println(this.Name, " use Paypal")
}
func (this Cregend) currency() {
   fmt.Println(this.Name, " use $")
}
// Cregend type implements the Staff interface.
func (this Cregend) getEmployeeInfo() {
   fmt.Println(this.Name, " employee_id is 10001")
}

// Synfun type implements the PayMethod interface
func (this Synfun) pay() {
   fmt.Println(this.Name, " use bank card")
}
func (this Synfun) currency() {
   fmt.Println(this.Name, " use $")
}
// Synfun type implements the Staff interface.
func (this Synfun) getEmployeeInfo() {
   fmt.Println(this.Name, " employee_id is 10002")
}

var p PayMethod //declare a variable p of type PayMethod interface
var s Staff     //declare a variable s of type Staff interface
cg := Cregend{
   Name: "Cregend",
}
sf := Synfun{
   Name: "Synfun",
}
p = cg //assign an instance cg to p
p.pay()
p.currency()
s = cg //assign the same instance cg to s
s.getEmployeeInfo()

p = sf
p.pay()
p.currency()
s = sf
s.getEmployeeInfo()

#Cregend  use Paypal
#Cregend  use $
#Cregend  employee_id is 10001
#Synfun  use bank card
#Synfun  use $
#Synfun  employee_id is 10002

From the above, it's evident that:

  1. A single type can implement multiple interfaces
  2. Multiple types can implement the same interface
  3. Interface-oriented programming is encouraged

Empty Interface

An empty interface is an interface that does not define any methods, which means that any type automatically implements the empty interface. This allows variables of the empty interface type to store values of any type.

var x interface{}

varString := "www.synfun.com"
x = varString
fmt.Printf("type:%T value: %v\n", x, x)

varInt := 100
x = varInt
fmt.Printf("type:%T value: %v\n", x, x)

varBool := true
x = varBool
fmt.Printf("type:%T value: %v\n", x, x)

//Using Empty Interface as Map Values
userInfo := map[string]interface{}{
   "username": "flasle",
   "sex":      "Male",
}
fmt.Printf("type:%T value: %v\n", userInfo, userInfo)

#type:string value: www.synfun.com
#type:int value: 100
#type:bool value: true
#type:map[string]interface {} value: map[sex:Male username:flasle]

Due to its ability to store values of any type, the empty interface is widely used in Go.

PS:Interfaces should only be defined when two or more concrete types need to be treated in a uniform manner. Avoid defining interfaces just for the sake of having interfaces; this can lead to unnecessary abstractions and runtime overhead.

Take a break

👉👉👉 【Jade Dynasty EP47】Gui Li and Xiao Bai went shopping, Jin Ping'er vs Lu Xueqi, Jin Ping'er vs Li Xun