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 analyze the methods in Go language

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

How to analyze the methods in Go language, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Concept

Methods in golang act on specific types of variables, so custom types can have methods, not just a struct structure.

The difference between method and function

But what's the difference between the method here and the function we introduced earlier?

Roughly, the small stack will explain to you that there is no concept of object in the function, so we call the object-oriented function methods, but they are all function in nature, but the function does not belong to any object, so as long as the parameters that meet the conditions can be called.

But it does not apply to the method. Because the method is limited to a certain object, the small stack will give you an example later.

Format

The format for methods in the Go language is as follows:

Func (receive type) methodName (args) (return parameter) {}

Taking an example we talked about earlier, we defined a car and a smallCar structure, where SmallCar inherits the properties of Car and, of course, the corresponding methods.

Please note, however, that we qualify Car when defining the method, which means that this Run method cannot be used except for Car and inheriting subclasses of Car. If you forcibly use the compiler, you will report an error directly, as shown in the figure:

In particular, it is important to note that distinguishing value and reference passing are still required in the go language, as shown in the following figure:

That is to say, this difference is also distinguished in the method. If you don't pay attention to it a little bit, it is likely to lead to the problem of troubleshooting for half a day and cannot be solved for beginners.

Similarly, the method also follows the difference between uppercase and lowercase. The scope of lowercase is limited to the current file, and we will capitalize other methods that need to be called externally. This can also be regarded as a specification of the Go language, and Xiao Zhanjun has also talked about this problem many times. I hope my friends will pay attention to this problem.

Inherit

Also similar to inheritance in structures, methods can also be inherited. As shown in the figure above, our SmallCar inherits the relevant properties of Car, and SmallCar also has methods unique to Car.

Combined and anonymous fields

If an struct nests another anonymous structure, then that structure can directly access the methods of the anonymous structure to achieve inheritance, if a structure has multiple anonymous structures nested.

Then this structure can directly access the methods of multiple anonymous structures to achieve multiple inheritance, which is not recommended in real projects.

If a problem is found, it will lead to the complexity of troubleshooting and the readability of the code.

After reading the above, have you mastered how to analyze the methods in the Go language? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Database

Wechat

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

12
Report