Selector of the Go language

Source: Internet
Author: User

For a primary expression x , which is a package name, the selector expression

X.f

denotes the field or method of the f value x (or sometimes *x ; see below). The identifier is f called the (field or method) selector; it must isn't be the blank identifier. The type of the selector expression is the type of f . If x is a package name, see the section on qualified identifiers.

A selector may f denote a field or method f of a type T , or it could refer to a field or method of f a Neste D anonymous field of T . The number of anonymous fields traversed to reach are f called their depth in T . The depth of a field or method f declared is T zero. The depth of a field or method declared in an anonymous field in are the depth of in f A T f A PLU S one.

The following rules apply to selectors:

  1. For a value x of type T or *T where T was not a interface type, x.f denotes the field or method at the SH Allowest depth in T where there are such an f . If There is not exactly one f with shallowest depth, the selector expression is illegal.
  2. For a variable x of type I where I was an interface type, x.f denotes the actual method with name f of The value assigned to x . If there is no method f I with a name in the method set of, the selector expression is illegal.
  3. In all and cases, is x.f illegal.
  4. If x is of pointer type and have the value nil x.f and denotes a struct field, assigning to or evaluating x.f Cau SES a run-time panic.
  5. If x is of interface type and have the value nil , calling or evaluating the method x.f causes a run-time panic.

Selectors automatically dereference pointers to structs. If x is a pointer to a struct, was x.y shorthand for (*x).y ; if the field y was also a pointer to a struct, x.y.z I s shorthand (*(*x).y).z for, and so on. If x contains an anonymous field of type *A , where was A also a struct type, is x.f shorthand for (*x.A).f .

Original Http://golang.org/ref/spec#Selectors

Selector of the Go language

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.