Go ' s block and identifiers scope

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

Go code block and identifier scopes

(Go ' s block and Identifiersscope)

First, Blocks

A block is a possibly empty sequence of declarations andstatements within matching brace Brackets.

statement block (Blocks) wrapped by closed curly braces, inside may contain empty statements or statements; 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 the source code in addition to the display Block ( specifically a block of code with {} parcels ) , there is also an implicit block.

1. the universe block encompasses all Go source text.

Go Language Global block, at the entire language level

2 . Each package have a package block containing all Go source text Forthat package.

each one Package have their own block, the source code in the parcel package

3. Each file have a File Block containing all Go source Text inthat fil E.

each file has its own block, the Block the role of more is Import Package Use

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

if, for, switch The statement has its own internal Block

5. each clause in a "switch" or "select" /c8>Statement acts as an implicitblock.

switch, select of each Case clause has its implied Block

Blocks Nest and influence Scoping.

Second, Declarations and Scope declarations and Scopes

Declaration binds a non-Blank identifier to a constant, type, variable, function, label, or  Package. Everyidentifier in a program must is declared. No identifier may declared Twicein the same block, and no identifier is declared in both the file Andpackage block.

a declaration binds a non-null identifier that is used to represent: constants, types, variables, functions, tags, packages. Each identifier must be declared before it is used. Again the sameBlock, identifiers cannot be declared repeatedly; identifiers are not allowed in thePackage Blockand theFile Blocknotice in each. Gofile, declare theIndentifierThe default scope is the entirePackage , Fileof theBlockis used only inImport Packagein use, so the consciousness of the last sentence is that the same fileBlockinidentifiercannot be declared two times in the same packageidentifiercannot be declared two times, iffilein that Package, theIderntifierin that Packageof all. Goonly one occurrence in the file)

the blank identifier may is used like any otheridentifier in a declaration, but it does not in Troduce a binding and thus isnot declared.

A blank identifier is just a placeholder, not a variable binding, like no declaration, is a syntactic sugar.

Declaration = constdecl | typedecl | vardecl .

topleveldecl = Declaration | functiondecl | methoddecl .

the scope of a declared identifier is the extent of source text Inwhich the identifier Deno TES The specified constant, type, variable, Function,label, or package.

the scope of an identifier is the indicator identifier in place of a specific constant,type, variable, function, label, or package The visible range in the Code field.

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

1. The scope of a predeclared identifier is the universe bloc K.

The pre-declared identifier is globally visible

2. the scope of an identifier denoting a constant, type,variable, or function (and not method) declared at Top level (outside Anyfunction) are the package block.

declared outside the function. constant,type, variable, or function (but not method) visible within the package scope

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

Import PackageName is scoped to the Import PackageName of the file

4. The scope of an identifier denoting a method receiver,function parameter, or result variable are the fun Ction body.

refer to method receiver,function parameter, or result variable The identifier scope is within the entire function;

5. the scope of a constant or variable identifier declaredinside A function begins at the end of the Const Spec or Varspec (shortvardeclfor short variable declarations) and ends at the end of the innermostcontaining block.

A reference that is declared inside a function onstant orvariable the scope of the identifier: starts at the life place, ends with the most internal Block

6. THe scope of a type identifier declared inside a function begins at Theidentifier in the TypeSpec a nd ends at the end of the innermost containingblock.

inside the function type type identifier scope: Starts at the life place and ends at the most internal Block end.

A identifier declared in a block could be redeclared in Aninner block. While the identifier of the inner declaration are in scope, itdenotes the entity declared by the inner declaration.

in the Block The identifier within the declaration, which can be Block of Internal Block , the re-declared identifier is only Inner Block visible.

the Package clause isn't a declaration; The packagename does not appear in any scope.< /c13>

Its purpose are to identify the files belonging to Thesame Specify the defaultpackage name for import declarations.

Package definition statement: Package PackageName is not a declaration, the package name does not belong to any scope, its purpose is to flag a file belonging to a package, the establishment of registration is to facilitate Import be introduced.

1. label Scopes label Scope

labels is declared by   labeled statements &NBSP; and is used in the &NBSP; &NBSP; "continue" &NBSP; "goto" &NBSP; statements. It is illegal todefine a label, which is never used. In contrast to other identifiers, labels Arenot block scoped and does not conflict with identifiers that is not labels. Thescope of a label is the body of the function in which it's declared andexcludes the body of any nested function.

The label name is declared in a label statement and is used in break, continue, goto statements. Defining a label instead of using it produces a syntax error. To differentiate from other identifiers, label does not apply to the block scope and does not conflict with other non-label identifiers, where the label scope is within its declared function, and the anonymous function inside the function is not visible to it.

Attention:

The declaration in go includes the meaning of the definition, declaring that a type is defined by declaring a variable that allocates storage space for it, and in go is called declare, not define.   Declare a value type variable go will allocate space for it, can be used directly, such as: Var int A; However, declaring a reference type requires allocating space for it before use, especially: Map, var MP map[string]sting, no MP, no need to use make to allocate space for it.

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.