Golang: Making Your Zero Values More Useful
Contents
[NOTE] Updated October 8, 2023. This article may have outdated content or subject matter.
Make the zero value useful. –Go Proverbs
Let’s start with the Golang blog: The zero value
When memory is allocated to store a value, whether by declaration or by calling make or new, and no explicit initialization is provided, the memory is given a default initialization. Each element of this value is set to its type’s zero value: false for booleans, 0 for integers, 0.0 for floats,
""
for strings, and nil for pointers, functions, interfaces, slices, channels, and maps. This initialization is done recursively, so, for example, if no value is specified, each element of a structure array will be zeroed.
Setting a value to zero in this way provides a great guarantee for the safety and correctness of the program, and also ensures the readability and simplicity of the program. This is what Golang programmers call “Make the zero value useful”.
Zero value cheat sheet
Type | Zero Value |
---|---|
bool | false |
int | 0 |
float | 0.0 |
string | "" |
pointer | nil |
function | nil |
slice | nil |
map | nil |
channel | nil |
The initialization of zero values is recursive, so if no value is specified, each element of the structure array will be zeroed.
|
|
The same is true for structures. We initialize a B structure that references A, and if no value is specified, each field of B will be zeroed.
|
|
note:
- new: new(T) returns a pointer to the
zero value
of the newly allocated T type. - The tool used is gore
Usage of Zero Values
The previous section has introduced what zero values are, here we will see how to use them.
sync.Mutex
Here is an example of sync.Mutex, sync.Mutex is designed to be used directly through zero values without explicit initialization.
|
|
Thanks to the characteristics of zero values, the two unexported variables inside Mutex will be initialized to zero values. So the zero value of sync.Mutex is an unlocked Mutex.
|
|
bytes.Buffer
Another example is bytes.Buffer, its zero value is an empty Buffer.
|
|
JSON omitempty
JSON receivers also accept the omitempty
flag, when the input field is a zero value
, the receiver will ignore this field.
|
|
channel close
In Channel Axioms, there is also a rule related to zero values, when the channel is closed, the <- operation on the closed channel always returns zero value
immediately.
|
|
The correct way to solve the above problem is to use a for loop:
|
|
Value not found for corresponding key in map
For a map, if the corresponding key is not found, the map will return a zero value of the corresponding type.
|
|
The solution to this problem is to return multiple values:
|
|
For non-existent keys, the value of ok will become false.
Summary
The above is some experience summary about zero value
. I hope everyone can use the zero value
better when designing code, and use the features provided by zero value
to initialize some variables.
Related Links
Article Recommendations
Finally, I would like to share with you some good articles I have been reading recently. I wanted to send them in a weekly format, but because I read them in a scattered way, I put them at the end of each blog post, hoping everyone can gain something.
- Why we don’t have children Some thoughts about having children
- Detailed interpretation of the paper “The Tail At Scale”
- Writing maintainable Go code Many points resonate with me after practice. The code is written once, but it will be read hundreds of times, so writing maintainable code is very important.
- Advanced Golang concurrency programming talk The sharer provides actual concurrency problems and then gives some of his own solutions. Very beneficial.
- pprof graphic explanation Finally, I can read pprof.
Sure, please provide the Markdown content you want to translate. I’ll make sure to follow the rules you’ve outlined.
Author xiantang
LastMod 2023-10-08 (049e9a9b)