1. What is a variable?
We should be how to understand the variables here I want to give an example?:
We should all know the glory of the king of this game. When we play king of glory, we control the hero's blood is constantly changing, the blood is the presence of memory. So this blood is a variable .
The program is running, the amount of the value can be changed, we called variables.
The above can be found by way of example:
- Variables are stored in memory
- Variables can be changed
We say that the variable is stored in memory, so memory, what is it?
Memory is a continuous collection of data in each memory storage area has a unique address identifier, called a memory address. We like the same ID, each person has a unique ID.
We know what the variables are, then the Go language, variables should be how to define it?
2. Variable
2.1 Definition of variables
Go basic variable definition is to use var
the keyword, the basic format is:
var 变量名 数据类型
Declare variables with keyword
var
at the beginning, end of line does not require a semicolon
For example, we define a name hp
, the type int
of variables:
var hp int // int表示为整型
Let's declare a variable and print it and see:
package main
import "fmt"
func main() {
// 使用 var 定义一个变量, 注意: 变量类型要放在变量名后面
var hp int
fmt.Println(hp)
}
// 结果:
0
Strange? I did not give a specific value hp, how to print out 0 it?
In fact, this is a feature Go: When we declare a default value for a variable time, if no initial value, Go give it a corresponding type.
In this case, we gave hp
a value of it.
package main
import "fmt"
func main() {
// 使用 var 定义一个变量, 注意: 变量类型要放在变量名后面
var hp int
hp = 100 // 赋值操作
fmt.Println(hp)
}
// 结果:
100
Of course, we can also directly when it was declared hp
a value:
var hp int = 100 // 定义变量并初始化值
If we simply define a variable and not to use it, then the Go will error.
2.2 define multiple variables
// 定义两个个类型都是int的变量
var hp, mp int
Since you can define multiple variables at the same time, it also can initialize multiple variables simultaneously
/*定义两个类型都是"int"的变量, 并且分别初始化为相应的值
hp为100, mp为260
*/
var hp, mp int = 100, 260
2.3 Automatic Derivation types
Have not found some complicated when we define a variable, you need to write 类型
, Go not allow us to write 类型
, it will automatically help us derive the corresponding data type:
var hp, mp = 100, 260
Some people say, that var
can also go, every time writing, good trouble no problem, Go also allows you to:
/*定义两个变量,它们分别初始化为相应的值
hp为100, mp为260
编译器会根据初始化的值自动推导出相应的类型
*/
hp, mp := 100, 260 // 注意这里有个冒号 :
Now I am not feeling very concise. :=
This symbol is a direct replacement for the definition and assignment variables.
hp := 100
// 等价于下面两行
var hp int
hp = 100
But it has a limitation that can be used only within the function; the function will not use external compiler, it is generally a var
way to define global variables.
2.4 anonymous variable
_
(Underscore) as the anonymous variable , it will discard the corresponding data is not processed. Anonymous variables with the function's return value to use value only, now we can to know their grammar.
In the following example, we have the value 2
given b
discarded3
_, b := 3, 2
Anonymous variable name do not take up space, it does not allocate memory.
Not because of repeated statements can not be used between variables and anonymous anonymous variable.
2.5 Variable naming convention
- Variables can only consist of letters, numbers, underscores.
- You can not start with a number.
- Go is not the keywords and reserved words
- Case sensitivity,
a := 1
andA := 1
the two variables.
The above requirements must be met, the following requirements to try to make
- Descriptive variable names have to be concise, easy to read, not too long.
- Variable names can not use Chinese and pinyin.
- Recommended variable names:
- Underline: my_name = 'Conan'
- Hump body: MyName = 'Conan'
Go language 25 Keywords:
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
Go language 37 reserved words:
Constants: true false iota nil
Types: int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
float32 float64 complex128 complex64
bool byte rune string error
Functions: make len cap new append copy close delete
complex real imag
panic recover