English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statements

Golang Functions & Methods

Golang Structs

Golang Slices & Arrays

Golang Strings(String)

Golang Pointers

Golang Interfaces

Golang Concurrency

Golang Exceptions(Error)

Golang Miscellaneous

Go Language Variable Scope

Prerequisites:Go Language Variable

The scope of a variable can be defined as a part of the program that can access a specific variable. Variables can be defined in classes, methods, loops, etc., like C / C ++SimilarlyIn Golang, all identifiers are lexical (or static) scope, which means the scope of a variable can be determined at compile time. Or you could say that a variable can only be called from the code block where it is defined.

The scope rules for Golang variables can be divided into two categories, depending on the location of the variable declaration:

  • local variable(Declared within a block or function)

  • global variable(Declared outside of a block or function)

local variable

  • Variables declared within a function or block are called local variables. These cannot be accessed outside the function or block.

  • These variables can also be declared within the for, while statements, and other internal statements of the function.

  • However, these variables can be accessed by nested code blocks within the function.

  • These variables are also called block variables.

  • If the same variable is declared twice with the same name in the same scope, a compilation error will occur.

  • These variables will not exist after the function execution is completed.

  • Variables declared outside the loop can also be accessed within nested loops. This means that methods and all loops can access global variables. Local variables can be accessed by the loop and execute the function within the function.

  • Variables declared within the loop are not visible outside the loop.

//local variable
package main
import "fmt"
//main function
func main() {
    //From here the local scope of the main function starts
    //local variable within the main function
    var myvariable1, myvariable2 int = 69, 145
    // display the value of the variable
    fmt.Printf("myvariable1 The value of the variable: %d\n, myvariable1)
    fmt.Printf("myvariable2 The value of the variable: %d\n, myvariable2)
} // Here the local scope of the main function ends

Output:

myvariable1 The value of the variable: 69
myvariable2 The value of the variable: 145

global variable

  • Variables defined outside of functions or blocks are called global variables.

  • These are available throughout the entire lifecycle of the program.

  • These are declared at the top of the program outside all functions or blocks.

  • These can be accessed from any part of the program.

//global variable
package main
import "fmt"
// global variable declaration
var myvariable1 int = 100
func main() {
    // local variable within the main function
    var myvariable2 int = 200
    //display the global variable
    fmt.Printf("global variable myvariable1 has the value: %d\n, myvariable1)
    //display the local variable
    fmt.Printf("local variable myvariable2 has the value: %d\n, myvariable2)
    //call the function
    display()
}
func display() {
    // display the global variable
    fmt.Printf("global variable myvariable1 has the value: %d\n, myvariable1)
}

Output:

global variable myvariable1 The value is: 100
local variable myvariable2 The value is: 200
global variable myvariable1 The value is: 100

Note:What will happen if there is a local variable with the same name as the global variable in the function?

The answer is simple, that is, the compiler will prefer the local variable. Usually, when two variables with the same name are defined, the compiler will produce a compilation error. However, if the variables are defined in different scopes, the compiler will allow it. As long as a local variable with the same name as the global variable is defined, the compiler will prefer the local variable.

Example:In the following program, you can clearly see the output. Since myvariable1is200, which is given in the function main. Therefore, it can be said that local variables have a higher precedence than global variables.

//Go program shows the compiler's precedence
//local variable on the global variable
package main
import "fmt"
//global variable declaration
var myvariable1 int = 100
func main() {
    //local variable within the main function
    //is the same as the global variable
    var myvariable1 int = 200
    // display
    fmt.Printf("Variable myvariable1 has the value: %d\n, myvariable1)
}

Output:

Variable myvariable1 The value is: 200