譯:go’s block and identifiers scope

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

go 代碼塊和標識符範圍

(go’s  block and identifiersscope)

一、      Blocks

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聲明和範圍

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 block和file block中聲明(注意在每個.go檔案中,聲明的indentifier預設的範圍就是整個package, file 的block僅僅被用來在import package中使用,所以上句話的意識就是,同一個檔案block中identifier不能聲明兩次,同一個包中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.  聲明一個value類型變數go會為其分配空間,可以直接使用,比如:var int a;   但是聲明一個參考型別需要進行先為其分配空間再使用,特別是:map,   var mp map[string]sting ;不能直接使用mp ,不需要使用make為其分配空間後才能使用。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.