Go languages: statically typed language, variables (variable) is a clear type, the compiler also checks the correctness of the variable type.
First, the basic type
Declare variables: global variables must have the keyword var
var name [type] specify the data type
var is a keyword to declare a variable, name is the variable name, type is the type of the variable. For example: var NUM_2 int = 10 ;
var name; do not specify the type of data, the specific data type inference by the system itself
var dg = "str"
var_name: = value does not specify the type of data, the specific data type inference by the system itself
dd := 1223
Batch statement:
was ( str_1 int db float64 )
The default value of the data
When a variable is declared, it is automatically given the value of zero types: int is 0, float to 0.0, bool is false, string is an empty string, the pointer is nil and the like. All memory is initialized in Go's.
Variable aliases
of the type ( // to type an alias string str str string // to float64 type an alias Double Double float64 ) func main() { // used as alias definition variable var . Price Double = 123.3435 std.Println(price) var name = STR " Joe Smith " std.Println(name) }
Variable type conversion
Unlike the java language can go implicit conversion, he must be displayed
main FUNC () { var NUM float32 = 1.2 // will turn float32 int var Bu int = int (NUM) std.Println(bu) }
Second, the constant
Declaration syntax definitions and variables constant format similar to:const name [type] = value
const PI = 3.14159 // approximation corresponds to the math.Pi
Batch definitions
const ( e = 2.7182818 pi = 3.1415926 )
iota constant generator
constant declaration can be used to initialize iota constant generator, which is used to generate a set of constants to initialize a similar rule, but do not write it again each line initialization expression. Const in a declaration statement, in the first line of the constant declaration where, iota will be set to 0, then add a line in each of a constant declaration.
const ( Sunday = iota Monday Tuesday Wednesday Thursday Friday Saturday ) func main() { std.Println(Sunday) std.Println(Monday) std.Println(Tuesday) std.Println(Wednesday) std.Println(Thursday) std.Println(Friday) std.Println(Saturday) }
Result:
0
1
2
3
4
5
6
Second, the statement:
// 1, ################## for ############# was fa int for { if fa> 10 { std.Println("fa >10") break } FA ++ } // 2, ################## for ############# was FFA int = 12 for FFA> 10 { std.Println("ffa >10") LHW - } //3、##################for############# for i := 0; i < 3; i++ { std.Println("for") }
var flag bool if flag { std.Println("=======true=======") } else { std.Println("=======false=========") } if a := 4; a > 1 { std.Println(a) }