In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
 
            
                     
                
2025-10-31 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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about

The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r


A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from

Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





 
             
            About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.