首页 > 代码库 > 译:go’s block and identifiers scope

译:go’s block and identifiers scope

go 代码块和标识符作用域

(go’s  block and identifiersscope)

一、      Blocks

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

语句块(blocks)由闭合的大括号包裹,里面可能包含空的声明或者语句;block与作用域密切相关,见下章节。

Block = "{" StatementList "}" .

StatementList = { Statement ";" }.

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

go源码中除了显示的block (特指有{}包裹的代码块),还有隐式block.

1.    The universe block encompasses all Go source text.

go 语言全局block,在整个语言层面

2.    Each package has a package block containing all Go source text forthat package.

每一个package有自己的block,包裹包中的源码

3.    Each file has a file block containing all Go source text inthat file.

每个文件有自己的block,block的作用更多的是import package使用

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

if , for , switch 语句有其内部的block

5.    Each clause in a "switch" or "select" statement acts as an implicitblock.

switch, select 的每个case子句有其隐含的block

Blocks nest and influence scoping.

二、      Declarations and scope声明和作用域

A declaration binds 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.

一个声明绑定一个非空标识符,用来表示:常量,类型,变量,函数,标签,包。每一个标识符在使用前必须进行声明。再相同的block中,标识符不能被重复声明;标识符不允许在package blockfile block中声明(注意在每个.go文件中,声明的indentifier默认的作用域就是整个package, file block仅仅被用来在import package中使用,所以上句话的意识就是,同一个文件blockidentifier不能声明两次,同一个包中identifier不能声明两次,如果file在该package中,则iderntifier在该package的所有.go文件中只能出现一次)

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

空白标识符仅仅用来占位,不进行变量绑定,就像没有声明一样,就是个语法糖。

Declaration   = ConstDecl |TypeDecl |VarDecl .

TopLevelDecl  = Declaration |FunctionDecl |MethodDecl .

The scope of a declared identifier is the extent of source text inwhich the identifier denotes the specified constant, type, variable, function,label, or package.

标识符的作用域是指标示符代替特定constant,type, variable, function, label, or package在代码域中的可见范围。

Go is lexically scoped using blocks: go 使用的是block来控制作用域(scope).

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

预声明的标识符是全局可见

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)在包范围内可见

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

import packagename  的作用域是该import packagename的文件

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

指代method receiver,function parameter, or result variable的标识符作用域在整个函数内部;

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

在函数内部声明的指代onstant orvariable的标识符的作用域:开始于生命处,结束与其所属的最内部的block

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类型标识符作用域:开始于生命处,结束于其所属的最内部block结尾。

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.

block内声明的标识符,可以在block内部的block内重新声明,重新声明的标识符仅在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 packagename不是一个声明,包名不属于任何作用域,它的目的是标志一个文件属于某个包,制定报名是为了便于import进行引入。

 

1.   Label scopes 标签作用域

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. Thescope of a label is the body of the function in which it is declared andexcludes the body of any nested function.

label名在标签语句中进行进行声明,在break, continue, goto 语句中使用。定义一个标签而不是用会产生语法错误。为了和其他标识符进行区分,label不适用与block 作用域,不会与其他的非label标识符产生冲突,label作用域在其声明的函数内,函数内部的匿名函数对其不可见。

 

注意:

go中的声明包括定义的含义,声明一个type就是在定义一个类型,声明一个变量就会为其分配存储空间;在go中统一称为declare , not define.