From: Robert Griesemer
+A block is a sequence of declarations and statements within matching
+brace brackets.
+
+In addition to explicit blocks in the source code, there are implicit blocks:
+
+Blocks nest and influence scoping (§Declarations and Scope).
+
-A declaration binds an identifier to a language entity such as
-a variable or function and specifies properties such as its type.
+A declaration binds an identifier to a constant, type, variable, function, or package.
Every identifier in a program must be declared.
+No identifier may be declared twice in the same block, and
+no identifier may be declared in both the file and package block.
-The scope of an identifier is the extent of source text within which the
-identifier denotes the bound entity. No identifier may be declared twice in a
-single scope, but inner blocks can declare a new entity with the same
-identifier, in which case the scope created by the outer declaration excludes
-that created by the inner.
+The scope of a declared identifier is the extent of source text in which
+the identifier denotes the specified constant, type, variable, function, or package.
-There are levels of scoping in effect before each source file is compiled.
-In order from outermost to innermost:
+Go is lexically scoped using blocks:
-The scope of an identifier depends on the entity declared:
+An identifier declared in a block may be redeclared in an inner block.
+While the identifier of the inner declaration is in scope, it denotes
+the entity declared by the inner declaration.
+The package clause (§Package clause) is not a declaration; the package name
+does not appear in any scope. Its purpose is to identify the files belonging
+to the same package (§Packages) and to specify the default name for import
+declarations.
+
+Labels are declared by labeled statements (§Labeled statements) and are
+used in the
-The following identifiers are implicitly declared in the outermost scope:
+The following identifiers are implicitly declared in the universe block:
+Blocks
+
+
+Block = "{" StatementList "}" .
+
+
+
+
+
+if
, for
, and switch
+ statement is considered to be in its own implicit block.switch
statement,
+ and each communication clause in a select
statement
+ acts as an implicit block.Declarations and Scope
-Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
+Declaration = ConstDecl | TypeDecl | VarDecl .
+TopLevelDecl = Declaration | FunctionDecl | MethodDecl .
-
-
if
, for
,
- or switch
statement, the
- innermost surrounding block is the block associated
- with that statement.Label scopes
- break
, continue
, and goto
+statements (§Break statements, §Continue statements, §Goto statements).
+In contrast to other identifiers, labels are not block scoped and do
+not conflict with identifiers that are not labels. The scope of a label
+is the body of the function in which it is declared and excludes
+the body of any nested function.
+Predeclared identifiers
Basic types:
@@ -1593,7 +1636,8 @@ A function declaration binds an identifier to a function (§Function types).
-FunctionDecl = "func" identifier Signature [ Block ] . +FunctionDecl = "func" identifier Signature [ Body ] . +Body = Block.
@@ -1612,10 +1656,6 @@ func min(x int, y int) int { func flushICache(begin, end uintptr) // implemented externally -
-Implementation restriction: Functions can only be declared at the package level. -
-@@ -1623,7 +1663,7 @@ A method declaration binds an identifier to a method, which is a function with a receiver.
-MethodDecl = "func" Receiver identifier Signature [ Block ] . +MethodDecl = "func" Receiver identifier Signature [ Body ] . Receiver = "(" [ identifier ] [ "*" ] TypeName ")" .@@ -1664,10 +1704,6 @@ its identifier may be omitted in the declaration. The same applies in general to parameters of functions and methods. -
-Implementation restriction: They can only be declared at package level. -
-
The type of a method is the type of a function with the receiver as first
argument. For instance, the method Scale
has type
@@ -1918,8 +1954,7 @@ It consists of a specification of the function type and a function body.
-FunctionLit = FunctionType Block . -Block = "{" StatementList "}" . +FunctionLit = FunctionType Body .
@@ -3218,10 +3253,7 @@ indicate that control should flow from the end of this clause to the first statement of the next clause. Otherwise control flows to the end of the "switch" statement. ---Each case clause acts as a block for scoping purposes -(§Declarations and scope rules). -
+A "switch" statement may include a simple statement before the expression. @@ -3505,10 +3537,6 @@ SendExpr = Expression "<-" Expression . RecvExpr = [ Expression ( "=" | ":=" ) ] "<-" Expression .
-Each communication clause acts as a block for the purpose of scoping -(§Declarations and scope rules). -
For all the send and receive expressions in the "select" statement, the channel expression is evaluated. Any expressions @@ -3973,13 +4001,11 @@ Each source file consists of a package clause defining the package to which it belongs, followed by a possibly empty set of import declarations that declare packages whose contents it wishes to use, followed by a possibly empty set of declarations of functions, -types, variables, and constants. The source text following the -package clause acts as a block for scoping (§Declarations and scope -rules). +types, variables, and constants.
-SourceFile = PackageClause { ImportDecl [ ";" ] } { Declaration [ ";" ] } . +SourceFile = PackageClause { ImportDecl [ ";" ] } { TopLevelDecl [ ";" ] } .
A source file gains access to exported identifiers (§Exported