This is a creation in Article, where the information may have evolved or changed.
Original: Https://golang.org/ref/spec#The_zero_value
The 0 value
When a variable or new value is created, if no initial value is explicitly specified for it, the go language automatically initializes its value to the 0 value for this type, and the 0 values for each type are as follows:
False:bool, 0:integer, 0.0:float, "": String, Nil:pointer, function, interface, slice, channel, map. For composite types, the Go language automatically initializes each element to the 0 value corresponding to its type recursively. For example: arrays, structs.
Original: When storage are allocated for aVariable , either through a declaration or a call of
new
, or when a new value was created, either through a composite literal or a call of
make
, and no explicit initialization is provided, the variable or value is given a default value. Each element of such a variable or value was set to the
Zero Value For its type:
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 do recursively, so for instance each element of the an array of structs would have it fields zeroed I F no value is specified.
These two simple declarations is equivalent (the following two declarations are equivalent):
var i intvar i int = 0
After
Type T struct {i int; f float64; next *t}t: = new (T)//a
The following holds://c
T.I = = 0t.f = = 0.0t.next = Nil
The same would also is true after (note: A and B variables are created in the same way, but each of its elements is automatically initialized by go with the type corresponding to the 0 value, equivalent to C)
var t t//b
Nil is specially prepared for the pointer type and reference type of the go language, so remember, haha; the last reminder: the array and struct of the go language is a value type, not a reference type Yo, like an array as a function parameter,
Because is a value type, so to copy yo, if the array of elements a lot, then the cost of replication is big, pay attention to it!
Note: I am a novice programmer of C + +, a graduate in the C + + years, the ability is not strong, but developed the root planing of the problem, the program written good, big face of the things we are all about, but for these details of things
Often do not pay attention, hide bugs on more, go language, although easy to learn a strong name, but still need to pay more attention to the details, so as not to drop the pit. Like the C + + pointer 0 value: 0, NULL, nullptr is its 0 value is not uniform, it is easy to make a bug.
Note: This article is only my personal notes, if there are errors and omissions, please correct me, study together, my e-mail: htyu_0203_39@sina.com