Zero Values in Go
In Go, all variables are guaranteed to have a zero value. It is mandated by the Language Specification and implemented by the compiler.
A Wild Example
Now let’s see it in the wild.
type Config struct {
// Level is the minimum enabled logging level. Note that this is a dynamic
// level, so calling Config.Level.SetLevel will atomically change the log
// level of all loggers descended from this config.
Level AtomicLevel `json:"level" yaml:"level"`
...
}
The Config struct has a field Level which is an AtomicLevel. So what’s in the AtomicLevel?
type AtomicLevel struct {
l *atomic.Int32
}
It’s a pointer field that points to a memory address of an atomic.Int32 value. All pointers are always initialized to nil, namely 0x0. This is also the first of the virtual memory addresses. This block of addresses are unmapped and the OS will step in for trying to access a protected zone leading to a segment fault.
Implications
Unlike other configurations, you will not be able to rely on defaults in this setup.