English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Voraussetzungen:Go Variablen
Der Gültigkeitsbereich einer Variable kann definiert werden als ein Teil des Programms, der auf eine bestimmte Variable zugreifen kann. Variablen können in Klassen, Methoden, Schleifen usw. definiert werden. Wie C / C ++GleichIn Golang sind alle Identifikatoren lexikalisch (oder statisch) gebunden, das heißt, der Gültigkeitsbereich einer Variable kann bei der Kompilierung bestimmt werden. Oder man könnte sagen, dass eine Variable nur aus dem Codeblock, in dem sie definiert wurde, aufgerufen werden kann.
Die Regeln für den Gültigkeitsbereich von Golang- Variablen können in zwei Kategorien unterteilt werden, abhängig von der Position der Variablen, an der sie deklariert werden:
local variable(Innerhalb von Block oder Funktion deklariert)
global variable(Außerhalb von Block oder Funktion deklariert)
Variablen, die in einer Funktion oder einem Block deklariert werden, werden als lokale Variablen bezeichnet. Diese können nicht außerhalb der Funktion oder des Blocks erreicht werden.
Diese Variablen können auch innerhalb von for, while-Statementen und anderen internen Anweisungen in der Funktion deklariert werden.
However, these variables can be accessed by nested code blocks within the function.
These variables are also called block variables.
If these variables are declared twice with the same name within 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 it.
Variables declared within a loop are not visible outside the loop.
//local variable package main import "fmt" //main function func main() { //From here on, the local scope of the main function begins //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 "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 its value is: %d\n, myvariable1) //display the local variable fmt.Printf("local variable myvariable2 its value is: %d\n, myvariable2) //call the function display() } func display() { // display the global variable fmt.Printf("global variable myvariable1 its value is: %d\n, myvariable1) }
Output:
global variable myvariable1 Der Wert ist: : 100 local variable myvariable2 Der Wert ist: : 200 global variable myvariable1 Der Wert ist: : 100
Note:What will happen if there is a local variable with the same name as the global variable within a function?
The answer is simple, namely 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 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 "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 its value is: %d\n, myvariable1) }
Output:
Variable myvariable1 Der Wert ist: : 200