The Go language method detailed

Source: Internet
Author: User
Tags function definition

method is a special function that binds to an object instance.

Used to maintain and present the state of the object itself. The object is introverted. The normal function focuses on the algorithm flow, by accepting parameters to complete a particular logical operation, and returning the final result, the method is associated with the state, the function is usually not.

The syntax difference between a method and a function definition is that the instance takes arguments and the compiler determines the type to which the method belongs. In some languages, although there is no definition, the function uses the implicitly passed this instance argument.

Methods can be defined for the current package, as well as any type other than interfaces and pointers. Method also does not support overloading, receiver parameter names are not limited. It is not recommended to use this and self. Methods can be considered as special functions, the type of receiver can naturally be the underlying type or pointer. This is related to whether the object instance is copied at the time of the call.

You cannot invoke a method with a multilevel pointer.

The receiver of the pointer type must be a legitimate pointer (including nil) or be able to obtain an instance address.


How to choose a receiver type for a method:

Use T:

1. Small objects or fixed values that do not need to modify the state.

2. Wrap the object as a pointer to a reference type, string, function, and so on.

Using *t:

1. The instance status needs to be modified.

2. Large objects use *t to reduce replication costs.

3. If you include a synchronization field such as a mutex, use *t to avoid the lock operation being invalidated by replication.

4. Other can not be determined to use all *t.


Anonymous fields:

Method will also have the same name masking problem. But with this feature, a similar overwrite operation can be achieved.

Method Set:

A type has a set of methods associated with it, which determines whether it implements an interface.

The collection of type T methods contains all receiver t methods.

The type *t method collection contains the receiver T + *t method.

The anonymous embedding S,t method set contains all receiver s methods.

The anonymous embedding *s,t method set contains all receiver S + receiver *s methods.

The anonymous embedding s or *s,*t method set contains all receiver S + *s.

An expression:

Methods can be divided into expression and value two method states.

(1) method expression:

A method expression that is referenced by a type is reverted to a normal function style, and the sink is the first argument that must be explicitly passed when invoked. As for the type, it can be T or *t, as long as the target method exists in that type of method set.

(2) Method value:

Based on an instance or a pointer-referenced method value, the parameter signature does not change and is still called in the normal way. However, when a method value is assigned to a variable or passed as a parameter, the receiver object that the method executes is immediately computed and copied and bound so that the sink object can be implicitly passed at a later execution.

The compiler generates a wrapper function for the method value, which implements the indirect call. As for receiver replication. And closures are implemented essentially the same way, packaged into Funval, and transmitted via DX registers. Of course, if the receiver of the target method is a pointer type, then only the pointer is copied. As long as the receiver parameter type is correct, the same can be done with nil.

Package Main


Import "FMT"


Type N int


Func Main () {

var number N = 100

Result: = Number.tostring ()

Fmt. PRINTLN (Result)//d

}


Func (number N) toString () string{

Return to FMT. Sprintf ("%s", string (number))

}

Operation Result:

D


Package Main


Import "FMT"


Type N int


Func Main () {

var a N = 25

A.value ()

A.pointer ()

Fmt. Printf ("A:%p,%v", &a, a)

}

Func (n N) value () {

n++

Fmt. Printf ("V:%p,%v\n", &n, N)

}

Func (n *n) pointer () {

(*n) + +

Fmt. Printf ("P:%p,%v\n", N, *n)

}

/*

Operation Result:

v:0xc04204c088, 26

p:0xc04204c080, 26

a:0xc04204c080, 2

*/


The Go language method detailed

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.