Home > Backend Development > Golang > Interfaces and polymorphism in Go language

Interfaces and polymorphism in Go language

王林
Release: 2023-06-01 21:40:34
Original
763 people have browsed it

With the rapid development of Internet technology, programming languages ​​​​are constantly updated and upgraded. Among them, Go language (Golang) is a programming language that has emerged in recent years. Due to its efficiency and ease of learning, it has been widely used in the field of Internet information technology. This article will focus on interfaces and polymorphism in Go language, hoping to help everyone better understand and apply Go language.

Interface is an important feature in Go language. In the Go language, an interface defines all the methods supported by an object. This means that if an object implements all methods defined in an interface, it can be considered an object of that interface type. An interface is like defining a set of specifications that an object must adhere to so that we can operate on the object regardless of its specific type.

In the Go language, the interface definition uses the interface keyword, and its basic form is:

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

For example, we define an interface named "Shape", which has a method named "Area ", returns an area value of type float64. Then the definition of the interface can be expressed as:

type Shape interface {
    Area() float64
}
Copy after login

After defining the interface, we need to let a type implement this interface. In Go language, using types to implement interfaces requires that the signatures of all methods in the interface be satisfied. For example, we define a rectangle type Rectangle, which has two float64 type fields width and height, which are used to represent the width and height of the rectangle. We need to define an Area() method for the Rectangle type to calculate the area of ​​the rectangle. The Rectangle type can implement the Shape interface. The sample code is as follows:

type Rectangle struct {
    width, height float64
}

func (r Rectangle) Area() float64 {
    return r.width * r.height
}
Copy after login

In the above code, we define a Rectangle type and implement the Area() method. Since our Rectangle type implements the Area() method defined in the Shape interface, we can operate it as a Shape type. For example, use a Shape type variable to represent a Rectangle type object.

In the Go language, the implementation of the interface is implicit. This means that as long as a type defines all the methods defined in the interface, it is automatically considered to implement the interface. The above Rectangle type automatically implements the Area() method in the Shape interface, so it can be regarded as implementing the Shape interface.

In the Go language, the implementation of polymorphism depends on the interface type. Polymorphism is the ability to determine the type of an object at compile time, but to determine the actual type of the object at runtime. In Go language, since interface is a specification, any type can be regarded as a type of that interface as long as it implements an interface. This allows us to use the same method name to operate different types of objects, thereby achieving polymorphism.

We can illustrate the implementation of polymorphism through a simple example. Suppose we have an Earth and Moon types, both of which implement a Rotation() method for calculating their rotation periods. We need to write a general function that accepts an interface that implements the Rotation() method and calculates the rotation period of the object. As shown below:

type Earth struct{}

func (e Earth) Rotation() float64 {
    return 24
}

type Moon struct{}

func (m Moon) Rotation() float64 {
    return 27.3
}

type RotationCalculator interface {
    Rotation() float64
}

func CalculateRotationTime(obj RotationCalculator) {
    fmt.Println("Rotation time:", obj.Rotation())
}

func main() {
    var earthObj RotationCalculator = Earth{}
    CalculateRotationTime(earthObj)

    var moonObj RotationCalculator = Moon{}
    CalculateRotationTime(moonObj)
}
Copy after login

In the above code, we defined an Earth type and a Moon type, both of which implement a Rotation() method. We also define a RotationCalculator interface, which requires the implementation of the Rotation() method. We use the CalculateRotationTime() function, which accepts a RotationCalculator object and prints the rotation period of the object. In the main function, we use Earth type and Moon type objects respectively to call the CalculateRotationTime() function and output the results. Since both Earth and Moon types implement the Rotation() method of the RotationCalculator interface, we can operate them as objects of the RotationCalculator type, achieving polymorphism.

In short, interfaces and polymorphism in the Go language are important features of this language. They allow us to perform common operations on different types of objects and achieve efficient code reuse. In actual programming, we need to deeply understand and apply interfaces and polymorphism in order to better utilize the features of the Go language and improve the efficiency and maintainability of the code.

The above is the detailed content of Interfaces and polymorphism in Go language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template