1. Variable declaration
To simply declare a variable, you can var
keyword, as follows:
var s string
The examples are merely declares a variable s
, type string
, and not initialize it, so it is string
a zero value, that is, "" (the empty string).
Let's try to declare a pointer type variable, as shown below:
var sp *string
Discovery is possible, but it also has not been initialized, so its value is *string
zero value type, that is nil
.
2. Variable initialization
Variable by =
operator assignment is to modify the value of the variable. If you assign a value to a variable when you declare it, this operation is called variable initialization.
If you want to initialize a variable, there are three ways.
- Initialize directly at declaration
var s string = "hello"
- Initialize after declaration
var s string
s = "hello"
- Use
:=
simple declaration
s := "hello"
3. Assignment of different types of variables
3.1 Value type variables
func main() {
var s string
s = "hello"
fmt.Println(s)
}
Run the above code, you can print normally, indicating that when the variable of the value type is not initialized, there is no problem with direct assignment.
3.2 What about pointer type variables
func main() {
var sp *string
*sp = "hello"
fmt.Println(*sp)
}
Run these codes, you will see the following error message:
panic: runtime error: invalid memory address or nil pointer dereference
This is because if there is no pointer type variable allocates memory on the default value is zero nil
, it does not point to memory, so it can not be used, the use of force will get more than nil
the wrong target.
For value types, even if you only declare a variable without initializing it, the variable will have allocated memory.
In the following example, we declare a variable s
, not its initialization, but can &s
get its memory address. This is actually a Go
language to help us to do, it can be used directly.
func main() {
var s string
fmt.Printf("%p\n",&s)
}
Why var wg sync.WaitGroup
variable declarations wg
are not initialized can also be used directly?
Because sync.WaitGroup
a struct
structure is a value type, Go
language automatically allocated memory, it can be used directly, not reported nil
anomaly.
Summary: If you want to assign a value to a variable, the variable must have a corresponding allocated memory, so that you can operate on this memory to complete the purpose of the assignment.
Note: In fact, more than an assignment for the pointer variable, if there is no memory allocation, the value of the operation will be reported as
nil
an exception, because memory is not operational.
Therefore, a variable must be declared and memory allocated before it can be assigned, and then it can be initialized when it is declared. When the pointer type is declared, the Go
language does not automatically allocate memory, so it cannot be assigned to it, which is different from the value type.
Therefore, map
and chan
, too, because they are essentially also a pointer type.
func main() {
var d map[string]string
d["aa"] = "123"
fmt.Printf("%v\n", d)
}
Error:
panic: assignment to entry in nil map