The basic grammar
Declare a variable
Var name type
The above three are syntax for declaring a single variable.
The variable name starts with a letter or underscore and consists of one or more letters, digits, and underscores
The definition of variable names is expected to be similar in almost all languages.
- Defining a single variable
The first way is to specify the type of the variable and use the default value if no value is assigned
var name type
name = value
Copy the code
In the second part, the Type inference was made according to the values.
If a variable has an initial value, Go will automatically be able to infer the type of the variable using the initial value. Therefore, if the variable has an initial value, the type in the variable declaration can be omitted.
var name = value
Copy the code
The variable to the left of =(short declaration) should not be already declared (if multiple variables are declared at the same time, ensure that at least one of them is new), otherwise it will cause a compilation error
name := value
/ / for
var a int = 10
var b = 10
c : = 10
Copy the code
Note that the third variable declaration can only be used inside functions, not global variables
package main
var a = "1"
var b string = "2"
var c bool
func main(a){
println(a, b, c)
}
Copy the code
The answer is 1, 2 false
- Multivariate definition
In the first case, the declaration and assignment are separated by commas, and the default value exists if no assignment is made
var name1, name2, name3 type
name1, name2, name3 = v1, v2, v3
Copy the code
Second, direct assignment. The following variable types can be of different types
var name1, name2, name3 = v1, v2, v3
Copy the code
Third, set types
var (
name1 type1
name2 type2
)
Copy the code
Pay attention to the problem
- Variables must be defined before they can be used
- Go is a static language that requires the same type of variable and assignment.
- Variable names must not conflict. (The same action domain cannot conflict)
- Short definition, at least one of the variable names on the left is new
- Short definition mode, cannot define global variables.
- The zero value of a variable. Also called the default value.
- If a variable is defined, it must be used, otherwise it will not compile.
We cannot use initialization declarations for variables with the same name again if they are in the same code block, for example: No new variables on left side of :=, but a = 20 is allowed because it will add a new value to the same variable.
If you use variable a before defining it, you get a compilation error undefined: a. If you declare a local variable but do not use it in the same code block, you will also get a compilation error, as in the following example:
func main(a) {
var a string = "abc"
fmt.Println("hello, world")}Copy the code
Attempts to compile this code will get the error A declared and not used In addition, it is not enough to simply assign a value to a, the value must be used.
If a variable with the same name already exists in the same scope, subsequent declaration initializations degenerate into assignment operations. At least one new variable must be defined in the same scope. For example, the following y is the new variable:
func main(a) {
x := 140
fmt.Println(&x)
x, y := 200."abc"
fmt.Println(&x, x)
fmt.Print(y)
}
Copy the code
The results
0xc000098008
0xc000098008 200
abc
Copy the code
Declare constants
A constant is an identifier of a simple value, an amount that will not be modified while the program is running. Declaration method:
const identifier [type] = value
Copy the code
Such as:
const b1 string = "100"
Copy the code
func main(a) {
const LENGTH int = 10
const WIDTH int = 5
var area int
const a, b, c = 1.false."str" // Multiple assignments
area = LENGTH * WIDTH
fmt.Printf("面积为 : %d", area)
println(a)println(a, b, c)
}
Copy the code
Running results:
Covers an area of:50
1 false str
Copy the code
Constants can be enumerated, groups of constants
const (
Unknown = 0
Female = 1
Male = 2
)
Copy the code
Note that if the type and initialization value are not specified in a constant group, it is the same as the rvalue of a non-empty constant on the previous line, for example:
func main(a) {
const (
x uint16 = 16
y
s = "abc"
z
)
fmt.Printf("%T,%v\n", y, y)
fmt.Printf("%T,%v\n", z, z)
}
Copy the code
Running results:
uint16.16
string,abc
Copy the code
Constant considerations:
- The data types in constants can only be Boolean, numeric (integer, floating point, and complex), and string
- Unused constants will not be reported at compile time
- When displaying the specified type, you must ensure that the left and right constant values are of the same type, and display type conversions can be performed if necessary. This is different from variables, which can be different types of values
Iota constants
Iota, a special constant, can be thought of as a constant that can be modified by the compiler. Iota can be used as an enumeration value: each time ioTA is used on a new line, its value is automatically incremented by 1.
func main(a) {
const (
a = iota / / 0
b / / 1
c / / 2
d = "ha" // ioTA += 1
e //"ha" iota += 1
f = 100 //iota +=1
g //100 iota +=1
h = iota //7, restore count
i / / 8
)
fmt.Println(a,b,c,d,e,f,g,h,i)
}
Copy the code
Running results:
0 1 2 ha ha 100 100 7 8
Copy the code
If the IOTA increment is interrupted, it must be explicitly restored. As in line 7 above.
const a0 = iota // a0 = 0 // const occurs, ioTA is initialized to 0
const (
a1 = iota // another const occurs, ioTA is initialized to 0
a2 = iota // a1 = 1 // const add line, iota + 1
a3 = 6 // a3 = 6 // define a constant
a4 // a4 = 6 // if no value is assigned, it is the same as the previous line
a5 = iota // a5 = 4 // const already added 4 rows, so this is 4
)
Copy the code
const (
OrderStatus = iota+1 / / 1
NoPay / / 2
PendIng // 3
Delivered / / 4
_ / / jump over 5
Received / / 6
)
Copy the code
fmt.Println(Received) / / print: 6
Copy the code