Blocks 块

原文:https://go.dev/ref/spec#Blocks

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

​ 块是一对匹配的花括号内可能为空的声明和语句序列。

Block = "{" StatementList "}" .
StatementList = { Statement ";" } .

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

​ 源代码中除了显式块之外,还有隐式块:

  1. The universe block encompasses all Go source text.
  2. 包含所有的Go源码文本的 universe block
  3. Each package has a package block containing all Go source text for that package.
  4. 每个都有一个package block,包含该包的所有 Go 源代码。
  5. Each file has a file block containing all Go source text in that file.
  6. 每个文件都有一个file block,包含该文件中的所有Go 源代码。
  7. Each “if”, “for”, and “switch” statement is considered to be in its own implicit block.
  8. 每个 “if"、"for “和 “switch “语句都被认为是在自己的隐式块中。
  9. Each clause in a “switch” or “select” statement acts as an implicit block.
  10. switch“或 “select “语句中的每个子句都是一个隐式块。

Blocks nest and influence scoping.

​ 块是可以嵌套并影响着作用域

最后修改 March 10, 2024: 更新 (ddf4687)