Home > Article > Backend Development > There are several types of variables in Go language
There are three types of variables: 1. Variables defined within a function are called local variables, and their scope is limited to the inside of the function; local variables do not always exist, they only exist after the function that defines them is called. This local variable will be destroyed after the function call ends. 2. Variables defined outside the function are called global variables. They only need to be defined in one source file and can be used in all source files; the global variable declaration must start with the var keyword. If you want to use the global variable in an external package The first letter of a variable must be capitalized. 3. The variables in the function definition are called formal parameters.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Go language is a statically typed language, so variables (variables) have clear types, and the compiler will also check the correctness of the variable type. In mathematical concepts, a variable represents a number that has no fixed value and can be changed. But from a computer system implementation perspective, a variable is one or more segments of memory used to store data.
A variable (constant, type or function) has a certain scope in the program, which is called scope.
Understanding the scope of variables is more important for us to learn the Go language, because the Go language will check whether each variable has been used during compilation. Once an unused variable appears, a compilation error will be reported. . If you cannot understand the scope of variables, it may cause some unexplained compilation errors.
According to the different definition locations of variables, they can be divided into the following three types:
Variables defined within a function are called local variables
Variables defined outside the function are called global variables
Variables in the function definition are called formal parameters
Let’s talk about them below introduce.
Local variables
Variables declared/defined inside a function are called local variables, and the scope of local variables is limited to the inside of the function. Variables defined inside the function, parameters and return values of the function, variables used inside the if and for structures, etc. are all local variables.
Local variables do not always exist. They only exist after the function that defines them is called. This local variable will be destroyed after the function call ends.
[Example] The following main() function uses local variables a, b, and c.
package main import ( "fmt" ) func main() { //声明局部变量 a 和 b 并赋值 var a int = 3 var b int = 4 //声明局部变量 c 并计算 a 和 b 的和 c := a + b fmt.Printf("a = %d, b = %d, c = %d\n", a, b, c) }
Use {} to limit the scope of variables
package main import "fmt" func main() { { name := "HaiCoder" fmt.Println("Name =", name) } }##We use { } Limit the scope of the variable name to {}, that is, the scope of the variable name at this time is only within {}, and name is a local variable. Note: If the variable is accessed outside the scope of the variable, the program will report an error
package main import "fmt" func main() { for i := 0; i < 3; i++{ fmt.Print(i) fmt.Print(" ") } fmt.Print(i) }We define a local variable i inside the for loop and use it, At this time, the scope of variable i is limited to the {} inside the for loop. Finally, when we access the variable i outside {} of the for loop, the program reports an error because the variable i exceeds the scope.
Global variables
The variables declared outside the function are called global variables. Global variables only need to be defined in a source file. It can be used in all source files. Of course, source files that do not contain this global variable need to use the "import" keyword to introduce the source file where the global variable is located before they can use this global variable. Global variable declarations must begin with the var keyword. If you want to use the global variable in an external package, the first letter must be capitalized. [Example] In the following code, line 6 defines the global variable c.package main import "fmt" //声明全局变量 var c int func main() { //声明局部变量 var a, b int //初始化参数 a = 3 b = 4 c = a + b fmt.Printf("a = %d, b = %d, c = %d\n", a, b, c) }
Note: The names of global variables and local variables in Go language programs can be the same, but local variables in the function body will be given priority.
package main import "fmt" //声明全局变量 var a float32 = 3.14 func main() { //声明局部变量 var a int = 3 fmt.Printf("a = %d\n", a) }
Formal parameters
When defining a function, the variables in parentheses after the function name are called formal parameters (referred to as formal parameters). Formal parameters will only take effect when the function is called, and will be destroyed after the function call is completed. When the function is not called, the formal parameters of the function do not occupy actual storage units and have no actual values. Formal parameters will be used as local variables of the function. [Example] Line 21 of the code below defines the formal parameters a and b.package main import ( "fmt" ) //全局变量 a var a int = 13 func main() { //局部变量 a 和 b var a int = 3 var b int = 4 fmt.Printf("main() 函数中 a = %d\n", a) fmt.Printf("main() 函数中 b = %d\n", b) c := sum(a, b) fmt.Printf("main() 函数中 c = %d\n", c) } func sum(a, b int) int { fmt.Printf("sum() 函数中 a = %d\n", a) fmt.Printf("sum() 函数中 b = %d\n", b) num := a + b return num }
The above is the detailed content of There are several types of variables in Go language. For more information, please follow other related articles on the PHP Chinese website!