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

Зміст курсу

Introduction to GoLang

Introduction to GoLang

1. Getting Started
2. Data Types
3. Control Structures
4. Functions
5. Arrays and Slices
6. Intro to Structs & Maps

bookLocal 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

copy
12345678
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:

go

index

copy
12345678910111213141516171819202122232425262728
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.

go

index

copy
12345678
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.

go

index

copy
1234567891011
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:

go

index

copy
1234567891011121314151617
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() }
In which scope should we put `var value int = 6` to avoid any errors?

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

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2
We're sorry to hear that something went wrong. What happened?
some-alt