Conteúdo do Curso
Introduction to GoLang
Introduction to GoLang
Local and Global Scopes
At this juncture, it's essential to delve into the concept of scopes to understand the areas where specific variables (or constants) can be accessed and where they cannot.
The scope of a variable or constant defines where it can be used.
When a variable or constant is declared outside of any code block, it becomes accessible throughout the entire program, earning it the label of having a global scope. In the following code snippet, the constant pi
exemplifies this global scope:
index
package main import "fmt" const pi float32 = 3.1415 func main() { fmt.Println(pi) }
Conversely, when a variable or constant is declared within a code block, it remains accessible solely within that specific code block and any nested code blocks, if applicable.
The illustration below delineates the distinctions between global and local scopes:
The following code demonstrates areas where variable access is permitted and where errors may occur:
index
package main import "fmt" func main() { var value_1 int = 1 // Condition 1 if(true) { var value_2 int = 2 // Condition 2 if (true) { var value_3 int = 3 fmt.Println("In Condition 2:", value_1) fmt.Println("In Condition 2:", value_2) fmt.Println("In Condition 2:", value_3) } fmt.Println("In Condition 1:", value_1) fmt.Println("In Condition 1:", value_2) fmt.Println("In Condition 1:", value_3) // Error here } fmt.Println("In Main:", value_1) fmt.Println("In Main:", value_2) // Error here fmt.Println("In Main:", value_3) // Error here }
The variable value_1
remains accessible throughout the main()
code block, including within nested code blocks like Condition 1 and Condition 2. It is also accessible within Condition 1 and its nested Condition 2.
However, attempting to directly access it outside of the main()
block will result in an error. Similarly, the variable value_2
, declared within Condition 2, is only accessible within that specific code block.
It's crucial to consider scopes in the context of functions. When we declare a variable or constant within a function, it remains confined to that function and is inaccessible in other functions, including main()
.
Note
main()
functions as an automatically executed function when the program is run.
index
func myFunc() { var number int = 7 fmt.Println(number) // Accessible here } func main() { fmt.Println(number) // Not accessible here }
Another crucial point to keep in mind is that we cannot declare two or more variables with the same name within the same scope or overlapping scopes.
index
func myFunc1() { var number int = 7 var number int = 9 // Error here } func myFunc2() { var number int = 7 if(2 > 1) { var number int = 9 // Error here } }
However, it is possible to declare variables with the same name in different scopes:
index
package main import "fmt" func myFunc1() { var number int = 7 fmt.Println("In Func 1:", number) } func myFunc2() { var number int = 9 fmt.Println("In Func 2:", number) } func main() { myFunc1() myFunc2() }
Obrigado pelo seu feedback!