The advanced path of Go language-object-oriented programming

Source: Internet
Author: User
Tags hypot

The advanced path of Go language-object-oriented programming

Yun Zhengjie

Copyright Notice: Original works, declined reprint! Otherwise, the legal liability will be investigated.

When you finish this article, I can say that your Golang is an introduction, what is the introduction? Is that you go to see the Docker source code can read 60% of the grammatical structure, because some of the UNIX codes may not have the Linux operation and maintenance of the students in the study will be very difficult, it seems to bring a certain degree of difficulty, if there is time I will give you to analyze the Docker department of the brilliant source code. Well, let's get back to the point, what is the content of our study today? That is, object-oriented programming. Of course, do not use the cock silk mentality: "If not the object of how to program it?" ", haha ~ what Jay is going to tell you is:" This object is not the object ", there is no" object "so programming Ah! So the question is, what is the object-oriented programming?

I. What is object-oriented programming;

objects in Golang can be summed up in one sentence: "Object-oriented is the method by which the data to be processed is bound to the function."

If you've never learned Python, you can understand that it is class, and if you've never spoken to other languages (like me) before learning Golang, then you can understand this: "It's a programming style that sees everything as an object, like people, cars, bread, and so on, Then the property variables that these objects have, such as age, ethnicity, work place, metamorphic period, longevity, and functions that manipulate these attribute variables are packaged into a class to represent, an abstraction of this class is an object, such as a person's class contains attributes such as age, name, address, etc. He also has some features for others to tell these attributes, such as: Say, look, go, etc.!! ”。 This is the object-oriented feature!!!

Two. Why should there be object-oriented programming;

When it comes to object-oriented programming, I have to tell you about process-oriented programming, which I shared with you about process-oriented programming, which defines functions, reduces the repetition of code, increases the extensibility and legibility of code, and so on. And when everyone "snapped" the code to knock out suddenly an object-oriented, the appearance of it will inevitably have doubts, the process has been so good, why also object-oriented? In fact, let's give an example to illustrate that you know. For example, let you write a human model, write with a function if you want to implement it? For example, now I want you to write about: "Andy Lau, Fan Bingbing, Jolin Tsai" their three characters, yes, you can use the function of process-oriented programming to define their characteristics, then the question comes, if I want to call "Andy Lau" or "Fan Bingbing" in the external characteristics of this if also realized? The use of functions can be very difficult to implement. Because process-oriented programming is not an impressive feature, but its complexity is low, there may be some lack of heat in the implementation of some functions.
At this time, the object-oriented thinking is out, object-oriented can be easily implemented in the external call "Andy Lau" or "Fan Bingbing" characteristics. Want to learn more about object-oriented development is can ask my master brother "Baidu", of course, you can also ask my second brother "Google", here do not pull too much history, we have to knock some code will reflect the benefits of object-oriented, because go is a completely object-oriented language. For example, it allows methods based on the type we define, without boxing/unpacking operations like in other languages. In fact , in contact with object-oriented programming at first everyone is rejected, there is a resistance mentality. But in a long time you will know which is what you want! It's like you're having fun with your male friends all day, and suddenly one day a very nice little sister appears in front of you, you say in your male friend's face: "This girl is just like that, look good", then notes brother secretly find the opportunity to date with this sister, eat, see the movie is a truth.

Three. If an object is defined;

When you see here, congratulations on your success has been brainwashed, if you still have resistance to learning object-oriented programming, it is recommended to close the page, because the inner conflict you are learning this blog content will be very difficult, may look at you do not understand, forget the previous process-oriented programming, Let's re-learn a new style of programming. Of course, we can also compare the difference between the two, so that learning is also easy to remember.

The following case is: In a two-dimensional space, find the distance between two points.

1. Use the function to achieve the distance of two points;

1/*2 #!/usr/bin/env Gorun3 @author: Yinzhengjie4blog:http://www.cnblogs.com/yinzhengjie/tag/go%e8%af%ad%e8%a8%80%e7%9a%84%e8%bf%9b%e9%98%b6%e4%b9%8b%e8%b7%af/5 Email:[email protected]6*/7 8 Package Main9 Ten Import ( One     "Math" A     "FMT" - ) -  the type point struct { - x, y float64 - } -  + func distence (p, Q point) float64 { -     returnMath. Hypot (Q.X-P.X,Q.Y-P.Y)//"Hypot"is to calculate the point distance between two points + } A  at Func Main () { -P: = point{1,2} -Q: = point{4,6} -Fmt. Println (Distence (P,Q))//the way the function is called, that is, the way the value is passed.  - } -  in  -  to #The above code output results are as follows: +5

2. Use the object to achieve the distance of two points;

1/*2 #!/usr/bin/env Gorun3 @author: Yinzhengjie4blog:http://www.cnblogs.com/yinzhengjie/tag/go%e8%af%ad%e8%a8%80%e7%9a%84%e8%bf%9b%e9%98%b6%e4%b9%8b%e8%b7%af/5 Email:[email protected]6*/7 8 Package Main9 Ten Import ( One     "Math" A     "FMT" - ) -  theType point struct {//define a structural body that you can understand is the class in Python - x, y float64 - } -  +Func (P point) distence (Q point) float64 {//to define a distence method for the P object, you can understand the method that binds a distence.  -     returnMath. Hypot (q.x-p.x,q.y-p.y) + } A  at Func Main () { -P: = point{1,2} -Q: = point{4,6} -Fmt. Println ((p.distence (q)))//the invocation of the class, note that if the definition is going to be called! (Here is the Distence method that calls p.) ) - } -  in  -  to #The above code output results are as follows: +5

When you look at these two pieces of code, you may now ask, the effect is the same, that is, they run the same results, but in the definition and invocation of different ways. Does not clearly reflect their differences. And they don't compare their differences. I just want to say that the fun is still behind us, and let's take a moment to understand its subtleties, and now we know how to define an object. What if the length of the multiple points is defined?

3. Small trial sledgehammer;

To calculate the total length of the lines for multiple points, the implementation code is as follows:

1/*2 #!/usr/bin/env Gorun3 @author: Yinzhengjie4blog:http://www.cnblogs.com/yinzhengjie/tag/go%e8%af%ad%e8%a8%80%e7%9a%84%e8%bf%9b%e9%98%b6%e4%b9%8b%e8%b7%af/5 Email:[email protected]6*/7 8 Package Main9 Ten Import ( One     "Math" A     "FMT" - ) -  the type point struct { - x, y float64 - } -  +  - func (P point) distence (Q point) float64 { +     returnMath. Hypot (q.x-p.x,q.y-p.y) A } at  -Func distence (Path []point) float64 {//defines a path that is variable, path is an array slice that contains a point type, and slice can be understood as an array of dynamically growing. - var s float64 -      forI: = 0;i <len (path)-1; i++ { -s + = Path[i]. Distence (path[i+1]) -     } in     returns - } to  + Func Main () { -Path: = []point{{10,20},{30,40},{50,60}} the FMT. Println (distence (path)) * } $ Panax Notoginseng  -  the #The above code output results are as follows: +56.568542494923804

Four.

The advanced path of Go language-object-oriented programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.