A designated value from the set of valid values of the type have to be chosen. What else could qualify more for the position to be the zero-value?
nil
is not a valid value of numerical types (such as int
, uint
, int32
etc.), so that's out of the question.
0
has the benefit that in binary representation it's full zeros. It's name is also in line with "zero value".
Maybe -1
? -1
in binary representation is full ones (using 2's complement representation). -1
is not any more special.
Then maybe the min value? In case of int32
that would be -1 << 31
which is -2147483648
. But if the type is unsigned, e.g. uint32
, that would again be 0
. Moreover, taking the type int
the min value is not even a constant as it depends on the target architecture (may be -1 << 31
and -1 << 63
).
If 0
is a value you use and you want to differentiate whether it has already been set (or if the value is just being the zero value), use a pointer (e.g. *int
) which may have a nil
value (the zero value) indicating it has not yet been set.
Another option is to use a second variable (may be of type bool
), it's true
value indicating the number has been set.
Also more techniques (choosing a clever, non-used valid value), check out How do I represent an Optional String in Go?