Course Content
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.go
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.go
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.go
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.go
However, it is possible to declare variables with the same name in different scopes:
index.go
Everything was clear?
Course Content
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.go
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.go
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.go
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.go
However, it is possible to declare variables with the same name in different scopes:
index.go
Everything was clear?