Go study notes (70)-Go variable declaration, variable initialization, value type variable assignment, pointer type variable assignment

1. Variable declaration

To simply declare a variable, you can varkeyword, as follows:

var s string

The examples are merely declares a variable s, type string, and not initialize it, so it is stringa 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 *stringzero 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.

  1. Initialize directly at declaration
var s string = "hello"
  1. Initialize after declaration
var s string
s = "hello"
  1. 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 nilthe 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 &sget its memory address. This is actually a Golanguage to help us to do, it can be used directly.

func main() {
    
    
   var s string
   fmt.Printf("%p\n",&s)
}

Why var wg sync.WaitGroupvariable declarations wgare not initialized can also be used directly?

Because sync.WaitGroupa structstructure is a value type, Golanguage automatically allocated memory, it can be used directly, not reported nilanomaly.

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 nilan 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 Golanguage does not automatically allocate memory, so it cannot be assigned to it, which is different from the value type.

Therefore, mapand 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

Guess you like

Origin blog.csdn.net/wohu1104/article/details/111242722