May 11, 2021 Go
Variables come from mathematics and are abstract concepts in computer languages that can store calculations or represent values. Variables can be accessed by variable names.
The Go language variable name consists of letters, numbers, and underscores, where the first letter cannot be a number.
The general form of declaring a variable is to use the var keyword:
var identifier type
First, specify the type of variable, and use the default value if not assigned after the declaration.
var v_name v_type v_name = value
Second, the variable type is determined on its own based on the value.
var v_name = value
Third, omitting var, note that the variable on the left should not have been declared, otherwise it would result in a compilation error.
v_name := value // 例如 var a int = 10 var b = 10 c : = 10
Here's an example:
package main var a = "w3cschoolW3Cschool教程" var b string = "w3cschool.cn" var c bool func main(){ println(a, b, c) }
The above examples perform as follows:
w3cschoolW3Cschool教程 w3cschool.cn false
//类型相同多个变量, 非全局变量 var vname1, vname2, vname3 type vname1, vname2, vname3 = v1, v2, v3 var vname1, vname2, vname3 = v1, v2, v3 //和python很像,不需要显示声明类型,自动推断 vname1, vname2, vname3 := v1, v2, v3 //出现在:=左侧的变量不应该是已经被声明过的,否则会导致编译错误 //类型不同多个变量, 全局变量, 局部变量不能使用这种方式 var ( vname1 v_type1 vname2 v_type2 )
Here's an example:
package main var x, y int var ( //这种只能出现在全局变量中,函数体内不支持 a int b bool ) var c, d int = 1, 2 var e, f = 123, "hello" //这种不带声明格式的只能在函数体中出现 //g, h := 123, "hello" func main(){ g, h := 123, "hello" println(x, y, a, b, c, d, e, f, g, h) }
The above examples perform as follows:
0 0 0 false 1 2 123 hello 123 hello
All basic types such as int, float, bool, and string are value types that use variables that point directly to values that are present in memory:
When you assign
=
of one variable to another, such as: j s
j = i
you actually copy the value of i in memory:
You can get the memory address of the variable i, for example, 0xf840000040 (each time the address may be different). T he value of the variable of the value type is stored in the stack.
Memory addresses vary from machine to machine, and even the same program has different memory addresses when executed on different machines. B ecause each machine may have a different memory layout, and the location allocation may be different.
More complex data typically requires multiple words, and the data is typically saved using reference types.
A variable of reference type r1 stores the memory address (number) where the value of r1 is located, or where the first word in the memory address is located.
This memory address is called a pointer, which is actually in another word.
Pointers to the same reference type can point to multiple words in a continuous memory address (the memory layout is continuous), which is one of the most efficient forms of storage, or they can be scattered in memory, each indicating the memory address where the next word is located.
When the assignment statement r2 is used, only references (addresses) are copied.
If the value of r1 is changed, all references to this value point to the modified content, and in this case r2 is affected.
We know that the type of variable can be omitted at the initialization of the variable and automatically inferred by the system, and that it is actually a bit redundant to write the var keyword on the declaration statement, so we can short-write them as a: .
The types of a and b (int and bool) are automatically inferred by the compiler.
This is the preferred form of using variables, but it can only be used in function bodies and not for the declaration and assignment of global variables. You can efficiently create a new variable, called an initialization declaration, using the operator: .
If in the same block of code, we can't use initialization declarations again for variables of the same name, for example: a: s 20 is not allowed, the compiler will prompt for the error no new variables on left side of :,, but a s 20 is available because it gives a new value to the same variable.
If you use variable a before you define it, you get a compilation error undefined: a.
If you declare a local variable but don't use it in the same block of code, you'll also get a compilation error, such as variable a in the following example:
func main() { var a string = "abc" fmt.Println("hello, world") }
Trying to compile this code will result in errors a publiced and not used.
In addition, it is not enough to simply assign a a, this value must be used, so use
fmt.Println("hello, world", a)
The error is removed.
However, global variables are allowed to be declared but not used.
>Multiple variables of the same type can be declared on the same row, such as:
var a, b, c int
Multiple variables can be assigned on the same line, such as:
a, b, c = 5, 7, "abc"
The above line assumes that variables a, b, and c have been declared, otherwise they should be used this way:
a, b, c := 5, 7, "abc"
These values on the right are assigned to the variables on the left in the same order, so the value of a is 5, the value of b is 7, and the value of c is "abc".
This is called parallel or simultaneous assignment.
If you want to exchange the values of two variables, you can simply use a, b, b, a.
The blank identifier is also used to discard values, such as value 5, which is discarded in: , b , 5, 7.
It's actually a write-only variable, and you can't get its value. T his is done because you have to use all the declared variables in the Go language, but sometimes you don't need to use all the return values that you get from a function.
Parallel assignments are also used when a function returns multiple return values, such as val and error err here, which are obtained simultaneously by calling the Func1 function: val, err , func1 (var1).