Object-oriented and pointer of Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

fornicated, come 10 sesame cakes ...

Package Mainimport"FMT"type Integerint//added new method for built-in int type less, object-oriented notationFunc (a integer) less (b integer)BOOL {    returnA <b}func integer_less (a integer, b integer)BOOL {    returnA <B}//Go object-oriented, if you need to modify the object, you must use the pointer, this is not a go constraint, or natural constraintsFunc (A *Integer) ADD (b Integer) {*a + =B}//The integer type is an int, an alias integer for int,//and added a series of methods Add,add2, and programmed a new type, which also fully has the function of int//passing values instead of pointersfunc (A integer) Add2 (b integer) {a+=FMT B. Println (a) fmt. Println (b)}func main () {varA Integer =1    ifA.less (2) {fmt. Println (A,"Less 2")    }    ifInteger_less (A,2) {fmt. Println (A,"Less 2")    }    //Add method is added to integer, and the Add () method needs to modify the value of the object, which requires a pointer reference//A.add (2)//FMT. Println ("A=", a)//the implementation member method is passed in if it is not a pointer, but a value, that is, an integer, not a *iteger, when a=1A.ADD2 (2) fmt. Println ("a=", a)//The go language, like the C language, is a type that is based on value passing, wants to modify the value of a variable, and can only pass pointers}/*There is no hidden this pointer method in the go language the target ("object") that is applied to the display is passed, and the target ("object") that is not applied by the hidden method does not need to be a pointer, nor does it have to be called this*/

Dairo said that the sesame cake was left at home

//value semantics and referential semantics//B=a//b.modify ()//If the modification of B does not affect the value of a, then this type is the value type//if the value of a is affected, then this type is the reference type//most of the go language types are value semantics//basic types: Byte, int, bool, float32, float64, string, etc.//composite types: array, struct (struct), and pointer (point)//the array is fully copied, which is the value semanticsFunc Main () {vara=[3]int{1,2,3}varB=a//represents a B=a assignment statement, which is a full copy of the array contentsb[1]++FMT. Println (A, b)}//reference method, you need to use pointersFunc Main () {varA = [3]int{1,2,3}    varb = &a//This assignment is a reference to the array contents, and the variable B type is the *[3]int typeb[1]++FMT. Print (A,*b)}

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.