In-depth exploration of Go methods (7.21 additions)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. 1) which types can have methods:
1) You can only write a method for a named type and a pointer to a named type;
2) cannot write a method to the interface type and the interface type pointer;
3) The method can only be written in the package that defines the named type.

2) Receiver is a method of value, and the compiler implicitly generates a receiver that is the same name as the corresponding type pointer. In turn, it doesn't.

3) Type restrictions for anonymous fields for structs:
1) can not be an unnamed type;
2) A pointer type that can be a named type or command type;
3) The interface type can be, the interface type of the pointer type does not work;

4) The rules of the method of the struct anonymous field are passed out:
1) When the anonymous field is a value type: The method of the value is passed to the value of the struct, and the method of the pointer is passed to the pointer of the struct;
2) When the anonymous field is a pointer type: The method of the pointer is passed to the value and pointer;
3) When an anonymous field is an interface type: The method is passed to the value and pointer;

5) The method of the anonymous field is a method that is wrapped and implemented as a peripheral structure.

6) When using a specific type to invoke the method.
First, the compiler looks at the type under which there is no method;
Second, the extension looks at the pointer type or base type of the type (if the type is a pointer type);
Finally, if none of the above lookups are found, an error is available.
Note: Extended view does not look at implicit implementations (methods that are passed by anonymous fields, values, and methods that are implicitly implemented by pointers)

7) A set of methods of a specific type is a collection of methods with that type of receiver.

8) The method set of the interface type is the method declared in its definition, the interface calls the method is similar to C + + call virtual function, the interface pointer has no method set.

9) The type only checks the set of methods when it is stored on the interface, and only the method set when invoking the method of the interface; The reflect package also only looks at the set of methods.

So:

Method of type A, both a and *a can be called;
The method of type A, although implicitly implements the method of type *a, **a cannot call;
The method of type *a, A, *a, **a can be called;
The method of **a cannot be realized;
Type A ... func (a A) X () {}func (a *a) Y () {}type IX interface{x ()}type IY interface{y ()}a: = a{}b: = &ac: = &ba. X ()//OKB. X ()//OKC. X ()//Errora. Y ()//OKB. Y ()//OKC. Y ()//Okvar IX IXIX = A//Okix = b//Okix = c//Errorvar iy Iyiy = A//Erroriy = b//Okiy = c//errorvar JX IXJX = IX  //OKJX = &IX//Error

For code:

Type I Interface {IE ()}type A IntFunc (_ A) VA () {}func (_ *a) PA () {}type b struct {I}func (_ B) VB () {}func (_ *b) PB () {}type c struct {a}func (_ C) VC () {}func (_ *c) PC () {}type d struct {*a}func (_ D) VD () {}func (_ *d) PD () {}



The following list illustrates the source of its method sets and methods:

Type (types) Explicit (an explicitly defined method)
implicit (implicit method of implementation)
inherit(methods that inherit from anonymous fields)
I Ie


*i


A Va


*a Pa Va

B Vb
Ie
*b PB Vb Ie
C Vc
Va
*c Pc Vc Pa,va
D Vd
Pa,va
*d Pd Vd Pa,va
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.