Home  >  Article  >  Backend Development  >  How to implement interfaces in Go language

How to implement interfaces in Go language

PHPz
PHPzOriginal
2023-04-05 13:47:343134browse

Go language (also called Golang) is an open source programming language developed by Google. It has the characteristics of efficiency, reliability and simplicity. An important feature of the Go language is the "interface". An interface is a collection of method signatures and is an abstract type. This article will introduce how to implement interfaces in Go language.

1. Definition of interface

In Go language, the definition of interface uses the keyword "interface", and the syntax format is as follows:

type 接口名 interface{
    方法名1(参数列表1) 返回值列表1
    方法名2(参数列表2) 返回值列表2
    ……
}

Among them, the interface name and method name are Naming follows the naming convention of the Go language. Method names are named in camel case. The first letter of the method name is capitalized to indicate that it is visible to the outside world. In an interface, the method name, together with the parameter list and return value list, is called the "method signature".

2. Interface Implementation

Interface implementation refers to the method code block that implements the interface based on the method signature defined by the interface. A method that implements an interface requires a method signature that meets the requirements of the interface. This method is called an "interface method."

The implementation of the interface requires the use of the unique type conversion mechanism of the Go language. Specifically, interface implementation needs to convert the implementation type (the type that implements the interface method) into the interface type (the type that implements the interface) by implementing the "interface method". In type conversion, the implementation type needs to implement all method signatures defined by the interface; any interface can implement the interface as long as all methods are implemented.

For example, we define an "animal" interface and two structures of "dog" and "cat", and implement the "interface methods" corresponding to these two structures. The code is as follows:

type Animal interface {
    Speak() string
}

type Dog struct {
}

func (d Dog) Speak() string {
    return "汪汪汪"
}

type Cat struct {
}

func (c Cat) Speak() string {
    return "喵喵喵"
}

func main() {
    var animal Animal //定义一个Animal类型变量
    animal = Dog{}     //将Dog类型的结构体转换为Animal类型的接口实现
    fmt.Println(animal.Speak())

    animal = Cat{}     //将Cat类型的结构体转换为Animal类型的接口实现
    fmt.Println(animal.Speak())
}

In the above code, we define an "Animal" interface, which contains the "Speak" interface method; at the same time, we define two types, namely "Dog" and "Cat" , and implement their corresponding "Speak" interface methods. In the main function, we implement the call of the "Speak" method by converting the "Dog" and "Cat" type variables into the "Animal" type interface implementation.

3. Multiple interface implementation

In the Go language, a structure can implement multiple interfaces, and an interface can be implemented by multiple structures at the same time. In this case, we need to use combination to implement multiple interfaces.

For example, we define an "animal" interface and a "pet" interface, and we implement the "animal" and "pet" interfaces in the "dog" structure respectively. The code is as follows:

type Animal interface {
    Speak() string
}

type Pet interface {
    Name() string
}

type Dog struct {
    name string
}

func (d Dog) Speak() string {
    return "汪汪汪"
}

func (d Dog) Name() string {
    return d.name
}

func main() {
    var animal Animal //定义一个Animal类型变量
    animal = Dog{name: "小明"} //将Dog类型的结构体转换为Animal类型的接口实现
    fmt.Println(animal.Speak())

    var pet Pet       //定义一个Pet类型变量
    pet = Dog{name: "小花"}   //将Dog类型的结构体转换为Pet类型的接口实现
    fmt.Println(pet.Name())
}

In the above code, we defined an "animal" interface and a "pet" interface, and implemented the interface methods corresponding to the "dog" structure. In the main function, we convert the "dog" structure into the "animal" interface implementation and the "pet" interface implementation respectively, thereby realizing the calling of the "Speak" and "Name" methods.

4. Summary

The interface in Go language is an important feature, which can achieve polymorphism and help us realize the idea of ​​interface-oriented programming. In Go language, the definition and implementation of interfaces are very simple, but you need to master the type conversion mechanism of Go language. This article briefly introduces interfaces and interface implementations in Go language, and explains the situation of multiple interfaces through examples.

The above is the detailed content of How to implement interfaces in Go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to escape golang urlNext article:How to escape golang url