This is a creation in Article, where the information may have evolved or changed.
4. Toad notes Go language-variables
The VAR statement defines a list of variables, just like the argument list of a function, the type is behind.
As seen in this example, the ' var ' statement can be defined at the package or function level.
The code is as follows:
Package Main
Import "FMT"
var c, Python, Java bool
Func Main () {
var iint
Fmt. Println (i,c, Python, Java)
}
The results of the implementation are as follows:
0 false false
Initialize variables
A variable definition can contain an initial value, and each variable corresponds to one.
If the initialization is using an expression, you can omit the type, and the variable gets the type from the initial value.
Package Main
Import "FMT"
var i, J int = 1, 2
Func Main () {
var C,python, Java = True, False, "no!"
Fmt. Println (I,j, C, Python, Java)
}
The results of the implementation are as follows:
1 2 True False no!
Short Declaration variable
In a function, the ': = ' concise assignment statement can be used instead of the VAR definition where it is explicitly typed.
Each statement outside the function must start with a keyword (' var ', ' func ', and so on), ': = ' structure cannot be used outside the function.
For example:
Package Main
Import "FMT"
Func Main () {
var i,j int = 1, 2
K: = 3
C,python, Java: = True, False, "no!"
Fmt. Println (I,j, K, C, Python, Java)
}
The results of the implementation are as follows:
3 true False no!