This is a creation in Article, where the information may have evolved or changed.
Variable
Variable declaration
The variable is declared by the keyword Var, and the data type is after the variable name.
var a intvar b int = 10
A variable declaration statement may not need to use a semicolon as a terminator.
Several variables that need to be declared can be placed together to avoid repeating the VAR keyword.
var ( a int b string)
Initialize variables
You can initialize a variable without declaring the data type, and the go compiler infers the type of the declaration based on the right value of the expression.
var a int = 10var a = 10
You can reduce the Write keyword var and the data type by using the: = operator, which explicitly expresses the variable declaration and initialization work at the same time.
Assigning values to variables
Go support多重赋值
var ivar ji, j = j, i
This will reduce the introduction of an intermediate variable.
var t = i;var i = j;var j = t;
Anonymous variables
You can use anonymous variables when the function returns multiple values, but only if you want to get individual return values, so you can omit the undefined variables.
func GetName() (firstName, lastName, nickName string) { return "May", "Chan", "Chibi Maruko"}
Take the value of nickname, you can write the following wording
_, _, nickName := GetName()
The advantage of using anonymous variables is that it can greatly reduce the complexity of communication and the difficulty of code maintenance.
Constant
Constants are values that are known and cannot be changed during compilation. (shaped, floating-point, plural type)
In go, 大写字母开头
the constant is in the 包外可见
.
Literal constants
The literal constants in go are untyped, as long as the constant is within the range of the corresponding type, it can be used as a constant of that type.
Defining constants
A constant const
is declared with a keyword, and a constant type can be defined, but it is not required, and if the definition does not specify a type, the constant is as untyped as the literal constant.
const Pi float64 = 3.14159265358979323846const zero = 0.0const ( size int64 = 1024 eof = -1)const a, b, c = 1, 2, "hello"
The value on the right side of a constant definition expression can be a constant expression at compile time, such as:
const Mask = 1 << 3
But because the assignment of a constant is a compile-time behavior, an rvalue cannot have any expression that requires a run-time to produce a result, such as:
const Home = os.GetEnv("HOME")
Pre-defined constants
Go built-in constants are true
, false
, iota
;
iota
is a constant that can be modified by the compiler, which is reset to 0 when each const keyword appears, and then does const
not appear once before the next occurrence, and iota
its value will increment by 1.
const ( c0 = iota // c0 = 0 c1 = iota // c0 = 1 c2 = iota // c0 = 2)
If the expression of more than two const
assignment statements is the same, then deliberately omit the assignment expression from the second start, because the code above can be abbreviated as:
const ( c0 = iota // c0 = 0 c1 // c0 = 1 c2 // c0 = 2)