This is a creation in Article, where the information may have evolved or changed. Declarations bind values to identifiers, such as packages, variables, types, and so on. Once the declaration is complete, it is necessary to know where the identifiers in the source reference the specified value (simply speaking, where an identifier is available). Go belongs to the lexical scope, so the parsing of identifiers depends on where it is declared in the code. This approach differs from the dynamic scope language in that the visibility of identifiers in a dynamic scope language does not depend on the declared location. Look at the following bash script: "' BASH#!/BIN/BASHF () {local v=1g}g () {echo" G sees V as $v "}h () {echo" H sees V as $v "}fgh" "variable v is in function f Defined, but because the function g is called by the function F, the function g can access V: ' >./SCOPE.SHG sees V as 1g sees V Ash sees V as ' ' when the function g is called separately or when G is called in function h, you can see V is not defined. Visibility in a dynamic-scope language is not static (the lexical scope is also called a static scope), but rather relies on the control flow. Compile error is reported when attempting to compile similar code for GO version: "' Gopackage mainimport" FMT "Func f () {V: = 1g ()}func g () {fmt. Println (v)//"Undefined:v"}func main () {f ()} ' Go's lexical scope uses a block of statements, so it is necessary to understand what a statement block is before learning the visibility rules. A statement block is a series of sequence of statements (an empty sequence is counted). Statement blocks can be nested, and are identified by curly braces. "' Gopackage mainimport" FMT "Func Main () {//start outer blocka: = 1fmt. Println (a) {//start inner blockb: = 2fmt. Println (b)}//END inner Block}//END outer block} "in addition to the indicated block of statements, there are some implicit statement blocks:-subject sentence block: Including all source code,- Package Statement block: Includes all the source code in the package (a package may include multiple files in a directory)-a file statement block: Includes all the source code in the file,-the For statement itself is also in its own implicit statement block: "' gofor I: = 0; ILt 5; i++ {fmt. Println (i)} "so the variable I declared in the initialization statement can be accessed in the condition statement, the post statement, and the nested block of the entire loop body. However, the use of I after the for statement causes an "undefined: I" compilation error. -The IF statement is also its own implicit statement block: "' goif I: = 0; I >= 0 {fmt. The Println (i)} ' if statement allows the declaration of a variable, which can be used in nested blocks when the condition is true, or in the Else statement block when the condition is false. -The switch statement is in its own implicit statement block: ' ' goswitch i: = 2; I * 4 {case 8:fmt. Println (i) default:fmt. PRINTLN ("Default")} "is similar to an if statement, you can use a temporary declaration variable as the beginning of a case clause. -the clauses in each switch statement are an implicit statement block. "' GoSwitch I: = 2; I * 4 {case 8:J: = 0fmt. Println (i, J) default://"J" is undefined herefmt. PRINTLN ("Default")}//"J" is undefined here "if the syntax specifies that each case clause belongs to the same syntax block, then this additional example is not required. This will force each clause to use curly braces, making the code less readable and concise. -each clause in the SELECT statement is an implicit statement block, similar to the clause in the switch statement: ' ' Gotick: = time. Tick (* time). Millisecond) loop:for {select {case <-tick:i: = 0fmt. PRINTLN ("tick", i) Break loopdefault://"I" is undefined herefmt. Println ("Sleep") time. Sleep (* time. Millisecond)}}//"I" is undefined here "" ["Scope in Go"] (https://medium.com/@mlowicki/scopes-in-go-a6042bb4298c) Explains the scope (visibility). Statement blocks play a key role in defining the entire mechanism of the scope. # # Resources-HTTPs://golang.org/ref/spec#blocks
Via:https://medium.com/golangspec/blocks-in-go-2f68768868f6
Author: Michałłowicki Translator: Sunzhaohao proofreading: polaris1119
This article by GCTT original compilation, go language Chinese network honor launches
This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove
380 Reads