variable
The variable declaration of GO language is similar to that of most languages. Variables are declared by the var keyword. However, as a statically typed language, go language needs to specify its type when declaring variables.
The following code declares a “name” variable of type “string” and assigns it a value of “Shenfq”.
var name string = "Shenfq"
Copy the code
If we do not assign, this variable will get a default value. Here are some basic types in go and their defaults.
type | The default value |
---|---|
Int (int32, int64……) | 0 |
Uint (uint32, uint64……) | 0 |
Float32, float64 | 0.0 |
bool | false |
string | “” |
Multivariable declaration
We can also use the var keyword to declare multiple variables:
// Declare two variables of the same type
var firstName, lastName string
// Assign values to both variables
firstName, lastName = "frank"."shen"
Copy the code
// Declare two variables as different types
var (
age int
name string
)
// Assign values to both variables
age, name = 25."Shenfq"
Copy the code
Type inference
If we assign a value to a variable during the declaration phase, we can omit the type of the variable because go will deduce its type from the initial value given during compilation.
var age = 25
fmt.Printf("Age type: %T", age) // the age type is int
Copy the code
var (
age = 18
name = "Shenfq"
)
fmt.Printf("Age type: %T", age) // the age type is int
fmt.Printf("Name type: %T", name) // The name type is string
Copy the code
Brief format
When we introduced variable declarations earlier, go can do type derivation at compile time if an initial value is given. In this case, go provides a simpler way to declare variables with := and omit the var keyword.
func main(a) {
age := 25
name := "Shenfq"
}
Copy the code
// It is also possible to assign multiple variables simultaneously
func main(a) {
age, name := 25."Shenfq"
}
Copy the code
⚠️ Note 1
This declaration can only be used inside functions, not global variables.
// ⚠️ cannot be used globally to declare variables in this way
age, name := 25."Shenfq"
// This method can only be used inside a function
func main(a) {
age, name := 25."Shenfq"
fmt.Printf("Age type: %T", age)
fmt.Printf("Name type: %T", name)
}
Copy the code
⚠️ Note 2
Variables that have already been declared cannot be assigned with :=.
func main(a) {
var age int
age := 25
}
Copy the code
Variables that have already been declared can only be assigned by =.
func main(a) {
var age int
age = 25
}
Copy the code
Global and local variables
In simple terms, variables declared outside a function are global variables, and variables declared inside a function are local variables.
Local variables that are declared and not used will not compile.
func main(a) {
var age int
}
Copy the code
However, global variables can be declared without use.
var age int
func main(a) {
name := "Shenfq"
//fmt.Printf("age type: %T", age)
fmt.Printf("Name type: %T", name)
}
Copy the code
In the code above, we declared the age global variable, but did not use it and compiled normally.
Blank identifier
As mentioned earlier, go can assign multiple variables at once when assigning values to variables. Also, the go function can return multiple results at once when returning.
func double(num int) (string.int) {
var err string
if num < 0 {
err = "Num cannot be negative"
return err, - 1
}
result := num * 2
return err, result
}
Copy the code
Above we implement a double function that takes a variable of type int (num) and returns two values, one for an exception and one for the result of num * 2. If num < 0, the system prompts that num cannot be negative.
func main(a) {
err, res := double(10)
iferr ! ="" {
fmt.Printf(err)
} else {
fmt.Printf("Result: %v", res)
}
}
Copy the code
If we don’t care about err, we just want to execute a double and print the result.
func main(a) {
err, res := double(10)
fmt.Printf("Result: %v", res)
}
Copy the code
After running, we receive a compilation error that the err variable is not used. In this case, the blank identifier (_) is needed.
func main(a) {
_, res := double(10)
fmt.Printf("Result: %v", res)
}
Copy the code
We can accept the err value as _, the value there will be discarded, and the compilation will go smoothly.
constant
A constant is a variable that does not change, once declared. In go, constants are declared by replacing the var keyword with the const keyword.
// Implicit type definition
const PI = 3.14
// Explicit type definition
const PI2 float = 3.14
Copy the code
Multiconstant declaration
Like variables, constants support multiple declarations at once.
func main(a) {
const (
PI = 3.14
PI2 = 3.14
)
fmt.Printf("Result: %v\n", PI)
fmt.Printf("Result: %v\n", PI2)
}
Copy the code
If multiple constants are declared at once and a constant is assigned, the value of the previous constant is synchronized by default. The following code runs the same as the above code.
func main(a) {
const (
PI = 3.14
PI2
)
fmt.Printf("Result: %v\n", PI)
fmt.Printf("Result: %v\n", PI2)
}
Copy the code
A special constant
There’s a special constant called IOTA, and when you assign a constant, you add it up.
func main(a) {
const (
A = iota
B
C
)
fmt.Println(A, B, C) / / 0 1 2
}
Copy the code
The IOTA can be interrupted during the accumulation process.
func main(a) {
const (
A = iota
B
C = "Shenfq"
D
E
)
fmt.Println(A, B, C, D, E)
}
Copy the code
In this case, the output is:
This is the result of changing constant C to the string “Shenfq”, and constants D and E are kept in sync with the previous constant by default. However, IOTA can be reaccumulated by reassigning the IOTA at the specified location.
func main(a) {
const (
A = iota
B
C = "Shenfq"
D = iota // Restore the accumulated state
E
)
fmt.Println(A, B, C, D, E)
}
Copy the code
Because C occupies the original position of 2, so after D is restored, it also starts from 3.
The cumulative nature of IOTA is very similar to enumerations we use in other languages, so in GO we can use IOTA directly as enumerations.
type ButtonType int
const (
Default ButtonType = iota
Primary
Warning
Error
)
Copy the code