Home  >  Article  >  Backend Development  >  A deep dive into the implementation of Golang interfaces

A deep dive into the implementation of Golang interfaces

PHPz
PHPzOriginal
2023-03-22 15:21:531498browse

Golang (also known as Go language) is a modern programming language designed and maintained by Google. Golang is a static language that improves code maintainability and robustness through strong typing and strict type checking. One of the most interesting features is interfaces, and this article will delve into the implementation of Golang interfaces.

What is the Golang interface?

Like other object-oriented programming languages ​​such as C and Java, Golang also supports interfaces. In Go, an interface is a declaration of a set of methods, and an interface type is a collection of types that must implement these methods. Simply put, an interface is an abstract type that defines method signatures but not implementations. An interface can contain 0 or more method signatures and does not contain any fields.

The interface can be compared to a card. The card stipulates certain usage rules, so that the person who implements the card interface knows that he must follow these rules for coding, which makes coding convenient. Performance and code flexibility have been improved.

Implementation of interface

It is very simple to implement an interface in Golang. A type only needs to implement all the methods in the interface type, and it can be called this interface type. implementation type. The process of defining and implementing an interface is as follows:

type interfaceName interface{
    method1(param1 type1, param2 type2, ...) (return1 type1, return2 type2, ...)
    method2(param1 type1, ...) (return1 type1, ...)
    ...
}

type myType struct {
    // myType 的一些域
}

func (t *myType) method1(param1 type1, param2 type2, ...) (return1 type1, return2 type2, ...) {
    // 方法体
}
func (t *myType) method2(param1 type1, ...) (return1 type1, ...) {
    // 方法体
}
// ...

In the above code, we define an interface interfaceName, which contains several methods. Then we defined a structure myType, which has some properties, and then we implemented the methods in interfaceName respectively. In this way, myType becomes the implementation type of interfaceName.

It should be noted that the implementation of the interface is non-intrusive, that is to say, we do not need to modify the defined types, we only need to define the methods to be implemented.

The relationship between interface types and implementation types

An interface type can create multiple implementation types. In other words, an interface type can be implemented by multiple types. Here is an example:

type Animal interface {
    Move() string
}

type Dog struct {}

func (d Dog) Move() string {
    return "I'm a dog, I can walk on 4 legs"
}

type Bird struct {}

func (b Bird) Move() string {
    return "I'm a bird, I can fly"
}

func main() {
    d := new(Dog)
    b := new(Bird)
    animal1 := Animal(d)
    animal2 := Animal(b)
    fmt.Println(animal1.Move())
    fmt.Println(animal2.Move())
}

In the above example, we defined an Animal interface and two types of implementations: Dog and Bird. Then we created an animal1 and animal2 objects, their types are both Animal, but the actual types they point to are Dog and Bird respectively. Finally, in the main function, the Move() methods of animal1 and animal2 are called respectively, and their movement modes are output according to the actual type behavior.

We can see that the Move() method of the actual type implements the Move() method of the interface type. As long as the type implements the interface, it can be called an implementation type.

Empty interface

The empty interface interface {} in Golang is a special interface. It does not have any methods and can represent any type, which is equivalent to java in Object class. Can be used to define parameters or return values ​​of any type, such as:

func foo(a interface{}) {
    switch a.(type){
    case string:
        fmt.Println("this is string type")
    case int:
        fmt.Println("this is int type")
    default:
        fmt.Println("unknown type")
    }
}

func main() {
    foo("hello")
    foo(42)
    foo(3.14159)
}

In the above example, we defined a foo function whose parameter is an empty interface type a. We use a switch statement to determine the actual type of a and respond accordingly. As you can see, the empty interface can receive any type of parameters.

Summary

The implementation of Golang interface is a very simple thing. As long as a type implements all methods in an interface type, it is called an implementation type of this interface type. An interface type can have multiple implementation types. The empty interface is a special interface that does not have any methods and can represent any type. In addition, the interface is non-intrusive, which greatly improves the flexibility and readability of writing code.

The above is the detailed content of A deep dive into the implementation of Golang interfaces. 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