This is a creation in Article, where the information may have evolved or changed.
declaring and assigning values
In the go language, declaring a variable can be var name type = expression done, but at the same time, Go also supports a form called a short variable declaration name := expression .
For convenience, Go supports multiple short variable declarations, which name, age := expression1, expression2 are easily confused with multiple variable assignments name, age = expression1, expression2 .
In addition, there is a new function to new(T) create an T object of a type.
If a multiple short variable declaration is used, once the variable has been declared within the same scope , the Go language actually performs an assignment, as an example.
package mainimport ( "fmt")var name string // 包级变量 namefunc main() { fmt.Print(&name) // 打印包级变量 name 的地址 var score int // 声明一个 score 的 int 变量 name, score := "Wolther47", 100 // 这一行中,有两个要点: // 1. score:score 在之前被声明为一个 int 变量,则对于变量 score,`:=` 相当于一个赋值语句 // 2. name:如果有一个变量在当前的作用域中,没有进行过声明,则 Go 就会进行声明,而不是向上寻找, // 即,此处的 name 会覆盖包级别变量 name. fmt.Printf("%s's score is %d.\n", name, score) fmt.Print(&name)}
The question arises as to how the Go language defines what is called a scope .
This is a more important issue, because Go will check at compile time for each variable to use, and once an unused variable is present, it will fail. If the scope is not understood, it is possible to introduce some unknown compilation errors.
In the Blocks and Scopes in Golang article, the author lists the four code blocks of Go:
- Universe block, containing all source code for the project
- Package block, containing all source code for the project, except for the imported packages
- File block, containing all the source code in the document, including the imported package
- Lexical block[1], the source code contained within the curly braces, but composite literals[2] and type definition type definitions does not constitute lexical block
For lexial block, there are some exceptions:
- The passed-in parameters of the function, as well as the result of the return, although outside the curly braces of the function body, their scope is considered inside the function body
- Select, loop
if , switch and for open two lexical blocks, one for explicit, the other for implicit, implicitly scoped scopes that are implicitly nested. The original text here is more abstract, the following if example directly on the code
package mainimport ( "fmt")type Cat struct { Name string Age int Servant string}func getCat() Cat { return Cat{"Sushi", 3, "Wolther47"}}func main() { if cat := getCat(); cat.Servant == "Wolther47" { // cat 这个变量的作用域从 if 关键词开始,一直到最后的花括号结束 words := "Yep" // words 这个变量的作用域只在花括号内 fmt.Printf("I've served %s for %d years.\n%s\n", cat.Name, cat.Age, words) }}
elseIt will also open a lexical block, and it is clear that the block should be in sibling relationship with the if explicitly opened lexical block, and also nested within if the implicit scope
switchcase(or default ) also opens a lexical block nested within the switch implicitly opened scope
In addition, strongly push this article, the author summed up the block more detailed, but also the nesting of blocks and the different elements where to declare the problem, here no longer repeat.
Published on WOLTHER47 's blog, this work is licensed under the Creative Commons Attribution 4.0 International license
The author of this article uses the local block to indicate that this article follows the Go programming Languageand still uses the lexical block
The Go programming Language of the mechanical industry press translates into "compound literal", which is not used by individuals who do not like this translation.