Hello everyone, I’m frank. Welcome to click on the blue text “Golang Language Development Stack” below the title to follow the public account. Set as a star to receive push articles for the first time. Scan the code at the end of the article and learn the Golang language together.
01
introduce
In the Go language, when a new value is created by declaring or by allocating storage space for a variable by calling new, or by composing a literal or by calling make.
If no explicit initialization is provided, a default value is specified for the variable or value, and each element of such a variable or value is set to a zero value of its type.
02
A zero value of type Golang
In the Go language, native types have default values, which are the zero values of the types.
It should be noted that zero-value initialization of the Go language type is done recursively, that is, each element of the array and struct is initialized with a zero value.
03
Golang defines the structures available with zero values
In the Go language, defining a structure with a zero value is not only important for the safety and correctness of the program, it can also be used directly without prior explicit initialization, making the Go program code more elegant.
Try to define the structs where zero values are available
For example, sync. in the Go standard library sync. A Mutex struct, which is a type of struct defined as available with zero values.
Because sync. Mutex contains two integer fields that are not exportable, so declare sync. When a value of type Mutex is used, the values of its fields are set to 0.
In our use of sync. Mutex can be used directly without prior explicit initialization.
However, it is important to note that sync. Variables of type Mutex cannot be copied after they are used.
The tile type is the type of zero value available
Although we mentioned in Part 02 that the zero value of a slice is nil, we know that for a variable with a value of nil, it needs to be pre-assigned (the available value) before we can use the variable.
But Golang’s tile type is special, it is a zero-value usable type, and we can append it directly without triggering an error referencing the nil.
Sample code:
Reading the above code, we declare an element of the integer slice type variable id, without explicitly initializing it in advance, directly use the append function to append the data for it, the result is that it can be executed normally, without triggering the error of referencing the nil variable.
However, slice types are available as types with zero values, but they also have limitations, and in the example code above, we cannot manipulate slice values by subscripting.
The zero value of the pointer type
Another special type is pointers, as we mentioned in Part 02, where the zero value of a pointer is also nil, but a nil pointer can call a method of its type.
Sample code:
04
summary
In this article, we describe that every native type of the Go language has a default value, which is a value of zero.
By enumerating the Go Standard Library sync. Mutex struct types, which describe the benefits of defining constructs available with zero values in the Go language.
Incidentally, there are two types of specialities in slices with zero values of nil and pointers.
Recommended Reading:
Resources:
Scan the QR code or reply to the “WeChat Group” to join the WeChat Group
“Like” and “watch” are the biggest support 👇
👇 For more exciting content, please click “Read the original article”