Translation: Go's block and identifiers Scope

Source: Internet
Author: User

Go code block and identifier Scope

(Go's block and identifierss)

I. Blocks

ABlockIs a possibly empty sequence of declarations andstatements within matching brackets.

The statement block is enclosed by closed braces, which may contain empty statements or statements. The block is closely related to the scope. See the following section.

Block = "{" statementlist "}".

Statementlist = {statement ";"}.

In addition to explicit blocks in the source code, thereare implicit blocks:

In the go source code, besides the displayed block (especially the code block with the {} package), there is also an implicit block.

1.Universe BlockEncompasses all go source text.

Global block of Go language, at the language level

2. Each package hasPackage BlockContaining all go Source Text forthat package.

Each package has its own block, and the source code in the package

3. Each file hasFile BlockContaining all go Source Text inthat file.

Each file has its own block, which is used by the import package.

4. Each "if", "for", and "Switch" statement is considered to be inits own implicit block.

If, for, and switch statements have internal blocks.

5. Each clause in a "Switch" or "select" Statement acts as an implicitblock.

Switch, each case clause of select has its implicit Block

Blocks nest and influence scoping.

Ii. Declarations and scope declaration and scope

ADeclarationBinds a non-blank identifier to a constant, type, variable, function, label, or package. everyidentifier in a program must be declared. no identifier may be declared twicein the same block, and no identifier may be declared in both the file andpackage block.

A declaration is bound with a non-null identifier to indicate constants, types, variables, functions, tags, and packages. Each identifier must be declared before use. In the same block, the identifier cannot be repeatedly declared; the identifier cannot be declared in the package block or file block (note that in each. in the go file, the declared indentifier's default scope is the entire package, and the file block is only used in the import package. Therefore, the first sentence is, identifier in the same file block cannot be declared twice, and identifier in the same package cannot be declared twice. If the file is in the package, iderntifier is in all of the package. the Go file can only appear once)

The blank identifier may be used like any otheridentifier in a declaration, but it does not introduce a binding and thus isnot declared.

The blank identifier is used only for placeholder, without variable binding. Just like no declaration, it is a syntactic sugar.

Declaration = constdecl | typedecl | vardecl.

Topleveldecl = declaration | functiondecl | methoddecl.

TheScopeOf a declared identifier is the extent of Source Text inwhich the identifier denotes the specified constant, type, variable, function, label, or package.

The scope of the identifier is the visible range of the indicator instead of the specific constant, type, variable, function, label, or package in the code field.

Go is lexically scoped using blocks: Go uses block to control scope ).

1. The scope of a predeclared identifier is the universe block.

The pre-declared identifier is globally visible.

2. The scope of an identifier denoting a constant, type, variable, or function (but not method) declared at top level (outside anyfunction) is the package block.

Constant, type, variable, or function (but not method) declared outside the function are visible within the package range.

3. The scope of the package name of an imported package isthe file block of the file containing the import declaration.

The scope of import packagename is the file of the import packagename.

4. The scope of an identifier denoting a method using ER, function parameter, or result variable is the function body.

It refers to the identifier scope of method aggreger, function parameter, or result variable within the entire function;

5. The scope of a constant or variable identifier declaredinside a function begins at the end of the constspec or varspec (variable vardeclfor short variable declarations) and ends at the end of the innermostcontaining block.

The scope of the identifier declared inside the function that refers to onstant orvariable: begins at the place of life and ends the innermost block to which it belongs.

6. tHe scope of a type identifier declared inside a function begins at theidentifier in the typespec and ends at the end of the innermost containingblock.

Type identifier scope inside a function: starts at the end of the lifecycle and ends at the end of the inmost internal block to which the function belongs.

An identifier declared in a block may be redeclared in aninner block. While the identifier of the inner declaration is in scope, itdenotes the entity declared by the inner declaration.

The identifier declared in the block can be re-declared within the block. The re-declared identifier is only visible in the inner Block.

The package clause is not a declaration; The packagename does not appear in any scope.

Its purpose is to identify the files belonging to thesame package and to specify the defaultpackage name for import declarations.

Package Definition Statement: Package packagename is not a declaration, and the package name does not belong to any scope. It aims to indicate that a file belongs to a certain package, and the registration is made to facilitate import.

 

1. Label scopes label Scope

Labels are declared by labeled statements and are used in the "break", "continue", and "Goto" statements. it is illegal todefine a label that is never used. in contrast to other identifiers, labels arenot block scoped and do not conflict with identifiers that are not labels. things of a label is the body of the function in which it is declared andexcludes the body of any nested function.

The label name is declared in the label statement and used in the break, continue, and goto statements. Defining a tag instead of using it produces syntax errors. To distinguish it from other identifiers, label is not applicable to block scopes and does not conflict with other non-label identifiers. Label scopes are within the declared functions, the anonymous function inside the function is invisible to it.

 

Note:

Declarations in go include definitions. Declaring a type is to define a type, and declaring a variable will allocate storage space for it. In go, declarations are called declare and not define.

 

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.