Go Language Object-oriented

Source: Internet
Author: User
Tags uppercase letter

The first time to develop the go language is because C + + features too complex, so that many C + + developers because of the characteristics of C + + headache, go language successfully streamlined C + + features, so it is very concise, very few features, but can accomplish a lot of things.

The go language is not like this class in the C++,java language, it only contains structures like the C language, with the structure and pointers and other characteristics to complete the role of a class, very clever use of pointers and structures, not only go object-oriented, including the Go language map and other operations are based on the structure. In fact, plainly speaking, C + +, Java and other object-oriented language, the underlying implementation of the class is a struct, the object is a reference to the pointer, but the language has encapsulated them. However, this makes many people not understand these things when they touch object-oriented.

Below, say what the object-oriented is written in go:

If we want to define a rect in Java, we can ask for the area, so we should write

 1  public  class   Rect { 2   int   X;  3  public  int   Y;  4  public  int   area () { 5  return  x*y;  6   7  }

It's simple, so what does the go language do? There is no so-called class in Go, all classes are represented by structs, so to write a class, we first need to define a struct:

1 struct {2     int 3 }

This is a rect struct, so there is not only a variable in a class, but also a member function, so the member function of Go is written like this:

1 int   {2     return r.x*r.y3 }

The function of this member function is to calculate the area, obviously, this member function value to the structure of the RECT is applicable, so that the so-called encapsulation, then, how do we create and initialize the class instance?

The go language offers a number of ways:

1 Rect: =new(rect)2 rect:=&rect{}3 rect: =&rect{1 ,2}4 rect: =&rect{x:3, y:4}

Then, once in a while, if you do not specify the size of the member variable, the Go language will default to initialize the member variable to the 0,bool type of false.

So, what about constructors?

We can write it this way:

1 int) *Rect {2     return &rect{x,y}3 }

In fact, this is our usual new object when the real operation, but go to really show him to us.

See here, we seem to have a question, that is, like Java, C + + for the description of visibility, the go language does not exist in the public and other keywords, go language directly Select the letter case control.

A variable that starts with an uppercase letter means that it is visible to other packages, and if you want to be invisible, use lowercase letters, but the visibility control in the go language is only for the package and not for the class, that is, the classes under the same package are visible. This time we can see why the output statement is written like this:

1 fmt. Println ("helloWorld")

Because this function is visible to other packages.

Go Language Object-oriented

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.