English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Prerrequisitos:Variables del lenguaje Go
El ámbito de una variable se puede definir como una parte del programa que puede acceder a una variable específica. Se puede definir una variable en una clase, método, bucle, etc., como C / C ++IgualEn Golang, todos los identificadores son de ámbito léxico (o estático), lo que significa que el ámbito de una variable puede determinarse en tiempo de compilación. O también se puede decir que una variable solo puede ser llamada desde el bloque de código donde se definió.
Las reglas de ámbito de las variables en Golang se pueden dividir en dos categorías, dependiendo de la ubicación de la declaración de la variable:
local variable(Declarado dentro del bloque o función)
global variable(Declarado fuera del bloque o función)
Las variables declaradas en una función o bloque se denominan variables locales. Estas no pueden ser accedidas fuera de la función o bloque.
Estas variables también pueden ser declaradas dentro de instrucciones for, while y otras en el ámbito de una función.
However, these variables can be accessed by nested code blocks within the function.
These variables are also called block variables.
If the same variables are declared twice within 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 within nested loops. This means that methods and all loops can access global variables. Local variables can be accessed by the loop and executed within the function.
Variables declared within a loop are not visible outside the loop.
//local variable package main import \ //main function func main() { //From here on, 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
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 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 El valor es: 100 local variable myvariable2 El valor es: 200 global variable myvariable1 El valor es: 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 myvariable1has the value200, 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 \ //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 El valor es: 200