Package Main
Import (
"FMT"
"Math"
)
Func Main () {
1. Define variable name age, do not initialize, use the default value of the corresponding type
var age int
Fmt. Println ("My Age was", age)
2. Assigning values to variables
Age = 29
Fmt. Println ("My Age was", age)
Age = 50
Fmt. Println ("My Age was", age)
3. Initialize values when defining variables
var height int = 160
Fmt. Println ("My height is", height)
4. Variable type inference: instead of explicitly specifying a variable type when defining a variable, the variable type is inferred based on the initialization value
var weight = 130
Fmt. Println ("My weight is", weight)
//5. Multiple variable values can be defined at the same time
Define multiple variables and initialize values at the same time
var myage, Myheight int = 29, 160
Fmt. Println ("My Age was", Myage, ", and My height is", myheight)
//5.1 If you initialize a value when you define multiple variables at the same time, you can infer the variable type based on the value of the variable without explicitly specifying the variable type
var hisage, hisheight = 29, 170
Fmt. Println ("His was", Hisage, ", and his height is", hisheight)
//5.2. Of course, you can also assign a value after you define the variable
var herage, Herheight int
Herage = 30
Herheight = 170
Fmt. Println ("Her age is", Herage, ", and she height is", herheight)
//5.3. In addition, you can define multiple variable values at the same time and assign different types of values
VAR (
name_ = "CCCC"
Age_ = 29
Height_ int
)
Fmt. Println ("Name_ is", name_, ", Age_ are", Age_, ", Height_ is", Height_)
//6. The Go language also provides a concise way to define variables, using the operator ": =" to implement
//In this way, the var symbol cannot be used when defining a variable: requires that all defined variables must be assigned at the same time, otherwise the error
_name, _age: = "dddd", 31
Fmt. Println ("_name is", _name, ", _age is", _age)
The error is defined as follows: No value is assigned to multiple variables defined at the same time
__name, __age: = "Eeeee"
Fmt. Println ("__name is", __name, ", __age is", __age)
//In addition, in this way, operator ": =" to the left of the variable: requires at least one is the most recently defined
I, J: = 10, 11
Fmt. Println ("i=", I, ", j=", J)
J, K: = 12, 13The k variable to the left of the//operator is up-to-date, satisfying the condition "at least one variable is newly defined"
Fmt. Println ("j=", J, ", k=", K)
J, K: =, the variable on the left of the operator is already defined and does not meet the "at least one variable" condition, run times wrong
Fmt. Println ("j=", J, ", k=", K)
//7. Variable initialization value can also be an expression value
A, B: = 10.0, 11.1
c: = Math. Min (A, B)
Fmt. Println ("A=", A, ", b=", B, ", and c=", c)
//8. Once the variable is assigned, the type is determined and no more values of the other type can be assigned
OK: = 20
Fmt. Println ("OK is", OK)
OK = "Hello"The type of the variable OK is determined after the value is assigned, and no other type of value can be assigned.
Fmt. Println ("Learning go language variable definition is complete!")
}
Learn the definition variables for the Go Language programming series