This is a creation in
Article, where the information may have evolved or changed.
Variables are also the basic elements of language, similar to the constants of the go language, constants are decorated with the Const keyword, and variables are decorated with the var keyword, and the variable declaration is in the form of var variableName variabletype
First, variable declaration
var age int
var phone int
var name string
var address string
var salary float64
var consume float64
var array [10]int
var frieds [20]string
Each variable declaration uses a var, can the above example reduce the number of Var? The previous said go is the most programmer-savvy, so of course, can be abbreviated as:
VAR (
Listen to age and listen to int
Listen to the phone and listen int
Listen to the name and listen to the string
Listen to the address string
Listen to salary listen to float64
Listen to consume float64
Listen to array listen [10]int
Listen to Frieds listen to [20]string
)
Although the above is declared with only one Var, but the number of rows of the variable is not reduced, the attentive reader may find that there are two int, two string, and two float64, can it be merged? The answer can be:
VAR (
Listen to age, phone listen int
Listen to name, address listen to string
Listen to salary, consume listen to float64
Listen to array listen and hear [10]int
Listen to Frieds listen and hear [20]string
)
When the type is the same, you can press var variable1,variable2,variable3,variable4 listen Variabletype This form declares
Second, the default value when declaring a variable
1. Variable default value
Run the above program can find the int variable default assignment to 0,string variable default assignment is empty string (so print out invisible), float64 variable default assignment to 0,int array of each element default assignment to the 0,string array default assignment to the empty string
Although there is no complex type defined in this example, it is not difficult to infer that the complex variable is assigned the default value 0+0i
2, the essence of the variable declaration
A variable declaration is essentially a request for a piece of data storage space and name it, so that the program can use the variable name of the storage space, the go compiler in the variable declaration by the way of assigning a default value
Third, variable initialization
As the example above shows, the compiler assigns a value by default when the variable is declared, but this value may not be what we want, so we can initialize the variable at the same time as the declaration, in the form of var variableName variabletype = value
var age int = 22
var phone int = 13581887557
var name string = "correct elder brother"
var address string = "Shandong"
var salary float64 = 3030.30
var consume float64 = 1552.8
var array [10]int = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var frieds [3]string = [3]string{"Shandong", "Li Dabong", "13581887557"}
Similar to a variable declaration, you can save the number of Var:
VAR (
Listen to age listen int listen to listen = 22
Listen to the phone listen int listen to listen = 13581887557
Listen to the name listen to string listen = "correct brother"
Listen to address string listen = "Shandong"
Listen salary listen to float64 listen = 3030.30
Listen consume float64 listen = 1552.8
Listen to array listen [10]int listen = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Listen frieds [3]string = [3]string{"Shandong", "Li Dabong", "13581887557"}
)
You can also reduce the number of rows:
VAR (
Listen to age, phone listen int listen to listen = 22, 13581887557
Listen to the name, address listen to string listen = "correct elder brother", "Shandong"
Listen to salary, consume float64 listen = 3030.30, 1552.8
Listen to array listen [10]int listen = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Listen frieds [3]string = [3]string{"Shandong", "Li Dabong", "13581887557"}
)
You can also remove variable types:
VAR (
Listen to age, phone listen = 22, 13581887557
Listen to name, address listen = "correct elder brother", "Shandong"
Listen to salary, consume = 3030.30, 1552.8
Listen to array listen = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Listen to frieds listen = [3]string{"Shandong", "Li Dabong", "13581887557"}
)
Is there any more ruthless? Readers ' eyes:
Age, Phone: = 22, 13581887557
Name, Address: = "correct elder brother", "Shandong"
Salary, consume: = 3030.30, 1552.8
Array: = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Frieds: = [3]string{"Shandong", "Li Dabong", "13581887557"}
Run, the small partners are stunned, so it is also OK, yes, go language is so wayward
Iv. Assigning values of variables
The following is the assignment process after declaring a variable:
var name string
Name = "Qingke"
Example:
If both the variable A and the variable B are integral, where a has a value of 10,b of 12, and the write program swaps the values of A and B
|
var a, b int
A, B = ten, //variable assignment
Fmt. Println ("a =", A, "B =", b)
A, B = B, a listen //A and B value interchange
Fmt. Println ("a =", A, "B =", b) |
Readers of the first contact with go may find that the digital exchange did not introduce intermediate variables! Yes, go is so willful
V. Variable declarations and initializers (: =)
" Example 1": First look at the applet
var value1 int32 = 22
value2: = 22
Fmt. Println (value1 = = value2)
Here first initializes the variable value1, which is of type int32, and then uses: = character to declare and initialize the variable value2, and finally determine whether value1 and value2 are equal?
The result of the operation was unexpected, and an error was thrown: saying that the type of value2 does not match the type of value1, that is, when initializing the variable value2, the go compiler automatically derives an int type to value2 based on the value 22 of the variable; in the Go language int, Int32 and Int64 are considered to be of different types.
" Example 2": Look at a small program again
var a int
A: = 23
will the program compile with an error?
From the running result, the use of: = variable definition and initialization of the same time, the second variable A and the first variable a name conflict.
This article is from the "Green Guest" blog, please be sure to keep this source http://qingkechina.blog.51cto.com/5552198/1615103