SoFunction
Updated on 2025-03-05

Go brain-burning interface and empty interface

Basic definition

Go official definition of interface is a sentence: An interface type is defined as a set of method signatures. Translated is that an interface defines a set of methods. This is similar to the interfaces of Java and PHP, defining a set of methods without defining the specific implementation of methods. But the very difference from Java and PHP is that Go does not require explicit declarations.implementsKeywords to inherit an interface. As long as a type implements all methods in the interface, it is considered to inherit the interface and is implicitly implemented. Let’s take a look at a basic usage example:

// Define a platform interface, including a payment methodtype Platform interface {
	Pay(amount int) error
}
// WeChat platformtype Wechat struct{}
func (w *Wechat) Pay(amount int) error {
	("wechat amount: %d\n", amount)
	return nil
}
// Alipay platform// Any value can implement an interface, and struct is not necessarytype Alipay int
func (a Alipay) Pay(amount int) error {
	("alipay amount: %d, a: %d\n", amount, a)
	return nil
}
func ExamplePlatform() {
	var (
		p Platform
		w        = Wechat{}
		a Alipay = 1
	)
	p = &w
	(2)
	p = &a
	(3)
	// This writing method will cause an error	// p = w
	p = a
	(4)
	// Output:
	// wechat amount: 2
	// alipay amount: 3, a: 1
	// alipay amount: 4, a: 1
}

In this example, we define aPlatformThe interface and two structures are respectively used to implement the value receiver and pointer receiver.PlatformInterface.p = wThis line of code will report an error. The reason is that for the interface implemented using the pointer receiverWechat, only itsPointers will implement interfaces, values ​​will not be implemented; and for value implementation interfaceAlipay, pointers and values ​​will implement interfaces. sop = aCan run normally.

Interface nesting

An interface can be nested with another interface:

// Define a platform interface, including a payment methodtype Platform interface {
	Pay(amount int) error
	User
}
type User interface {
	Login()
	Logout()
}
// WeChat platformtype Wechat struct{}
func (w *Wechat) Pay(amount int) error {
	("wechat amount: %d\n", amount)
	return nil
}
func (w *Wechat) Login()  {}
func (w *Wechat) Logout() {}

at this time,WechatIt's realizedPlatformThe interface has also been implementedUserInterface.

Interface type assertion

Let’s take a look at a very complex example. We will slightly modify the above code andWechatofLoginandLogoutmentioned to another structure, and then use type assertion to judgeWechatIs it implementedUserInterface:

// Define a platform interface, including a payment methodtype Platform interface {
	Pay(amount int) error
	User
}
type User interface {
	Login()
	Logout()
}
type UserS struct {
}
func (u *UserS) Login()  {}
func (u *UserS) Logout() {}
// WeChat platformtype Wechat struct {
	UserS
}
func (w *Wechat) Pay(amount int) error {
	("wechat amount: %d\n", amount)
	return nil
}
func ExamplePlatform() {
	var (
		p Platform
		w = Wechat{}
	)
	p = &w
	(2)
	// Type assertion	_, ok := p.(User)
	(ok)
	// Output:
	// wechat amount: 2
	// true
}

Empty interface

Go 1.18 has added a new variable type:any, its definition is as follows:

type any = interface{}

In fact, any is an empty interface. For an empty interface, it has no method, so it is equivalent to implementing an empty interface for any type of value. This concept is very similar to another programming concept. It is a famous generic. In Go,The received value of the function is exactly oneany

func Println(a ...any) (n int, err error) {
	return Fprintln(, a...)
}

Using an empty interface with type assertion, we can design a simple type conversion function that converts arbitrary value into an int:

func ToInt(i any) int {
	switch v := i.(type) {
	case int:
		return v
	case float64:
		return int(v)
	case bool:
		if v {
			return 1
		}
		return 0
	case string:
		vint, _ := (v)
		return vint
	}
	return 0
}

This is the end of this article about the brain-burning interface in Go. For more related Go interface content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!