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) } }
package main import "fmt" func main() { for i := 0; i < 3; i++{ fmt.Print(i) fmt.Print(" ") } fmt.Print(i) }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Efficiently handle concurrency security issues in multi-process log writing. Multiple processes write the same log file at the same time. How to ensure concurrency is safe and efficient? This is a...
