Methods and uses of polymorphism in Golang

WBOY
Release: 2023-12-29 13:57:41
Original
1028 people have browsed it

Methods and uses of polymorphism in Golang

How to use interfaces to achieve polymorphism in Golang

In Golang, interface is a powerful feature that can achieve polymorphism, that is, an object can be Presented in different forms. Using interfaces to achieve polymorphism can improve the scalability and maintainability of your code. This article will explain how to use interfaces to achieve polymorphism in Golang by introducing the definition of interfaces, implementation of interfaces, and examples of polymorphism.

Definition of interface

In Golang, an interface is a collection of methods. An interface defines the signature of a collection of methods, not the implementation. The definition of the interface generally uses the type and interface keywords, for example:

type Animal interface {
    Eat()
    Sleep()
}
Copy after login

The above code defines an Animal interface, which contains two methods: Eat() and Sleep().

Implementation of interface

To implement an interface, you need to define an entity type for it and implement the methods defined in the interface for the entity type. Entity types need to meet the method signature requirements of the interface, that is, have all methods defined by the interface. The following code demonstrates how to implement the Animal interface:

type Dog struct {
    name string
}

func (d Dog) Eat() {
    fmt.Println(d.name, "is eating")
}

func (d Dog) Sleep() {
    fmt.Println(d.name, "is sleeping")
}
Copy after login

In the above code, we define a Dog structure, which contains an attribute named name. Then, we defined two methods, Eat() and Sleep(), for the Dog type, implementing the methods defined by the Animal interface.

Implementation of polymorphism

Using the polymorphism of the interface, we can realize that the same interface is called by different entity types. The following code shows how to use the polymorphism of the interface:

func Action(a Animal) {
    a.Eat()
    a.Sleep()
}

func main() {
    dog := Dog{name: "旺财"}
    Action(dog)
}
Copy after login

In the above code, we define an Action() function that receives a parameter of Animal interface type. The interface methods Eat() and Sleep() are called in the Action() function. In the main function, we create an instance of Dog type dog and pass it as a parameter to the Action() function. Since the Dog type implements the methods of the Animal interface, the Action() function can be called successfully.

By running the above code, we can see the following output:

旺财 is eating
旺财 is sleeping
Copy after login

The above output shows that the Dog type instance dog successfully called the Eat() and Sleep() methods of the Animal interface. Polymorphism is implemented between different entity types.

Summary

Through the definition, implementation and polymorphism examples of interfaces, we show how to use interfaces to achieve polymorphism in Golang. Interfaces allow different entity types to call the same interface type, thereby improving the scalability and maintainability of the code. Using interfaces, we can write more flexible and efficient code.

The above is the detailed content of Methods and uses of polymorphism in Golang. 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