Golang's hands

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

First on the code

Package Mainimport"FMT"type MyIntint//Anonymous Functions//the receive of the value//Receiving of pointers//*myint represents the pointer type of Myint//*i represents the pointer I, the value that points to IFunc (i *myint) Add (anotherint) myInt {*i = *i +MyInt (another)return*i}//I1 and I are two variables, passing valuesFunc Main () {i1:= MyInt (1) I2:= I1.add (2) fmt. Println (I1,I2)}

Value method, the pointer method follows the following rules:

The value represented by the recipient variable is actually a copy of the source value. If this value is not a pointer type, there is no way to change the source value in the value method.

The pointer value and its copy point to the same value, so there is a way to change the source value in the pointer method.

If the recipient type is a reference type or its alias type, even if it is a value method, you can change the source value

-------------

For a non-pointer data type, only its value method is included in the collection of methods associated with it. For its pointer type, the method collection contains only its value method, which also contains the pointer method.

On a value other than the pointer data type, it is also possible to invoke its pointer method.

This is because go has been automatically converted internally.

For example, if the Add method is a pointer method, the expression I1.add (2) is automatically converted to (&I1). Add (2)

The second rule is important for writing an implementation type for an interface type.

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.