Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Local and Global Scopes | Functions
course content

Course Content

Introduction to GoLang

Local and Global ScopesLocal 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:

go

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:

go

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.

go

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.

go

index.go

However, it is possible to declare variables with the same name in different scopes:

go

index.go

In which scope should we put var value int = 6 to avoid any errors?

Select the correct answer

Everything was clear?

Section 4. Chapter 2
course content

Course Content

Introduction to GoLang

Local and Global ScopesLocal 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:

go

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:

go

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.

go

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.

go

index.go

However, it is possible to declare variables with the same name in different scopes:

go

index.go

In which scope should we put var value int = 6 to avoid any errors?

Select the correct answer

Everything was clear?

Section 4. Chapter 2
some-alt