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

Tutoriel de base en Golang

Instructions de contrôle en Golang

Fonction & Méthode en Golang

Structure en Golang

Coupe & Tableau en Golang

Chaîne (String) en Golang

Pointeur en Golang

Interface en Golang

Concurrence en Golang

Exception (Error) en Golang

Autres articles divers en Golang

Portée des variables en Go Lang

Prérequis :Variables du langage Go

La portée d'une variable peut être définie comme une partie du programme qui peut accéder à une variable spécifique. Les variables peuvent être définies dans des classes, des méthodes, des boucles, etc., comme en C / C ++Même choseDans Golang, tous les identifiants sont de portée léxicale (ou statique), c'est-à-dire que la portée des variables peut être déterminée à la compilation. Ou vous pouvez dire qu'une variable ne peut être appelée que depuis le bloc de code où elle est définie.

Les règles de portée des variables en Golang peuvent être divisées en deux catégories, en fonction de l'emplacement de la déclaration de la variable :

  • local variable(Déclaré à l'intérieur du bloc ou de la fonction)

  • global variable(Déclaré à l'extérieur du bloc ou de la fonction)

local variable

  • Les variables déclarées dans une fonction ou un bloc sont appelées variables locales. Elles ne peuvent pas être accédées en dehors de la fonction ou du bloc.

  • Ces variables peuvent également être déclarées à l'intérieur des instructions for, while, etc. des fonctions.

  • 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 in the same scope with the same name, 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 in nested loops. This means that all methods and loops can access global variables. Local variables can be accessed by loops and can execute functions within the function.

  • Variables declared inside a loop are not visible outside the loop.

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

Output:

myvariable1 Variable value: 69
myvariable2 Variable value: 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 \
// global variable declaration
var myvariable1 int = 100
func main() {
    // local variable inside the main function
    var myvariable2 int = 200
    //display the global variable
    fmt.Printf("global variable myvariable1 value is: %d\n", myvariable1)
    //display the local variable
    fmt.Printf("local variable myvariable2 value is: %d\n", myvariable2)
    //call the function
    display()
}
func display() {
    // display the global variable
    fmt.Printf("global variable myvariable1 value is: %d\n", myvariable1)
}

Output:

global variable myvariable1 La valeur est : 100
local variable myvariable2 La valeur est : 200
global variable myvariable1 La valeur est : 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 there is a local variable with the same name as the global variable, 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 priority than global variables.

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

Output:

variable myvariable1 La valeur est : 200