Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Polymorphism in Go language

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to achieve polymorphism in Go language", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve polymorphism in Go language" article.

What is polymorphism?

I believe that students who have studied the object-oriented language Java are no stranger to polymorphism. When the code is executed, they can execute the methods in the subclass according to the type of the subclass. Polymorphism refers to the ability of code to take different behaviors according to the specific implementation of the type. If a type implements an interface, values of this type can be supported everywhere that interface is used.

Examples of Polymorphism in Go language

In such a scenario, we involve a lot of notification events in application development. The types of notifications can be Wechat, QQ, Email, etc., then we can abstract an API and define an API for notification, and then Wechat notification class, QQ notification class and Email notification class implement the corresponding notification methods respectively.

Define a notification type: notifier

/ / Notifier interface type Notifier interface {/ / Notification method, which can be implemented by a specific class notify ()}

Define the Wechat type, QQ type, Email type, and implement the notify method, with the pointer type as the recipient of the method

Type WechatNotifier struct {Name string Message string} func (w * WechatNotifier) notify () {fmt.Printf ("% v notify% v\ n", w.Name, w.Message)} type QQNotifier struct {Name string Message string} func (Q * QQNotifier) notify () {fmt.Printf ("% v notify% v\ n", q.Name Q.Message)} type EmailNotifier struct {Name string Message string} func (e * EmailNotifier) notify () {fmt.Printf ("% v notify% v\ n", e.Name, e.Message)}

Define the method of sending notification. If the input parameter is Notifier;, if you need to call it, you need to pass in the type of the interface that implements Notifier.

Func sendNotify (notifier Notifier) {notifier.notify ()}

Test it

Func main () {w: = & WechatNotifier {Name: "Wechat", Message: "Wechat message",} Q: = & QQNotifier {Name: "QQ", Message: "QQ message",} e: = & EmailNotifier {Name: "Email", Message: "Email message" } / / accept Wechat type sendNotify (w) / / accept QQ type sendNotify (Q) / / accept Email type sendNotify (e)}

The test results are as follows:

The above is about the content of this article on "how to achieve polymorphism in Go language". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report