Variables and constants are an essential part of the program, also part of the well understood.
Identifiers and keywords
Identifier
Identifier is the word has a special meaning programmer-defined in a programming language, such as variable names, constant names, function names, and so on. Go language alphanumeric identifier and _
(underscore), and can only letters and _
beginning. A few examples: abc
, _
, _123
, a123
.
Keyword
A keyword is a programming language predefined identifier good that has special meaning. Keywords and reserved words are not recommended as a variable name.
Go language has 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
In addition, Go language there are 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
variable
Variable origin
The program is running in the data are stored in memory, we want to operate in the code you need to find variables memory on some data, but if we directly from your code through a memory address to manipulate variables, code readability will be very poor but also prone to error, so we use a variable to hold up this data memory address, directly after the memory will be able to find the corresponding data through this variable.
Variable Types
Variable (Variable) functions to store data. Save different types of variable data may be different. After half a century of development, the programming language has been basically formed a set of fixed type, common variable data types are: integer, float, boolean and so on.
Go language in each variable has its own type, and variables must begin transitioning to a statement.
Variable declaration
Go language variables in the need to declare to use, it does not support the same scope repeat statement. Go after the variable declaration and the language must be used.
Declaration of
Go language variable declaration format is:
var 变量名 变量类型
Variable declaration keyword var
beginning, variable type on the back of variables, end of the line do not need a semicolon. for example:
var name string
var age int
var isOk bool
Batch Statement
Each declare a variable you need to write var
keyword would be more cumbersome, go language also supports batch variable declarations:
var (
a string
b int
c bool
d float32
)
Variable initialization
Go language when declaring variables, memory area will automatically initialize variables corresponding operation. Each variable is initialized to the default value of its type, for example: Default integer and floating point variable value 0
. The default value of string variable 空字符串
. Boolean variables default false
. Slice, functions, variables for the default pointer nil
.
Of course, we can also assign initial values when declaring variables. Variable initialization standard format as follows:
var 变量名 类型 = 表达式
for example:
var name string = "Q1mi"
var age int = 18
Or a more variable initialization
var name, age = "Q1mi", 20
Type inference
Sometimes we will omit the type of variables, the compiler will type this time to derive the value of a variable based on the right of the equal sign to complete the initialization.
var name = "Q1mi"
var age = 18
Short variable declaration
Inside the function, you can use a more simple :=
way to declare and initialize variables.
package main
import (
"fmt"
)
// 全局变量m
var m = 100
func main() {
n := 10
m := 200 // 此处声明局部变量m
fmt.Println(m, n)
}
Anonymous variable
When using multiple assignment, if you want to ignore a value can be used 匿名变量(anonymous variable)
. Anonymous variable with an underscore _
, for example:
func foo() (int, string) {
return 10, "Q1mi"
}
func main() {
x, _ := foo()
_, y := foo()
fmt.Println("x=", x)
fmt.Println("y=", y)
}
Anonymous variable name do not take up space, does not allocate memory, so there is no duplication between the anonymous variable declarations. (In Lua
other programming languages, the anonymous variable is also called the dummy variables.)
Precautions:
- Each statement outside the function must begin with the keyword (var, const, func, etc.)
:=
You can not be used outside the function._
Used for placeholder, omit values.
constant
With respect to the variable value of the constant is constant, those values are used for the definition of the program does not change during operation. Constant declarations and variable declarations are very similar, just var
replaced const
, constants must be assigned at the time of definition.
const pi = 3.1415
const e = 2.7182
Declared pi
and e
after two constants, their values do not change happen again during the entire program run.
You can also declare multiple constants together:
const (
pi = 3.1415
e = 2.7182
)
When a plurality of constants declared const Meanwhile, if the value is omitted, and the above values represent the same row. E.g:
const (
n1 = 100
n2
n3
)
In the example above, the constant n1
, n2
, n3
the values are 100.
iota
iota
Language is a constant go counter can only be used in constant expressions.
iota
When the const keyword appears it will be reset to zero. const constant declarations of each new row will iota
count time (IOTA appreciated row index block is const statement). Use iota can simplify the definition, when you define enumeration useful.
for example:
const (
n1 = iota //0
n2 //1
n3 //2
n4 //3
)
Some common iota
examples:
Use _
skip value
const (
n1 = iota //0
n2 //1
_
n4 //3
)
iota
Middle statement to jump the queue
const (
n1 = iota //0
n2 = 100 //100
n3 = iota //2
n4 //3
)
const n5 = iota //0
Define the number of stages (here <<
represented by left shift operation, 1<<10
represents a binary representation of 10 to the left, i.e. from the 1
became 10000000000
, 1024. Similarly i.e. decimal 2<<2
representation of the binary representation to the left by 2 2, and is made 10
into a 1000
, i.e. decimal 8.)
const (
_ = iota
KB = 1 << (10 * iota)
MB = 1 << (10 * iota)
GB = 1 << (10 * iota)
TB = 1 << (10 * iota)
PB = 1 << (10 * iota)
)
Multiple iota
definitions in a row
const (
a, b = iota + 1, iota + 2 //1,2
c, d //2,3
e, f //3,4
)