In addition to Oleiade, see spec for null values :
When memory is allocated to store a value, either through an declaration, or by a make call, or by a new one, and explicit initialization is not provided, the default initialization is assigned to the memory. Each element of this value is set to zero for its type: false for booleans, 0 for integers, 0.0 for float, "" for lines and nil for pointers, functions, interfaces, slices, channels and maps. This initialization is performed recursively, therefore, for example, each element of the array of structures will have its own fields if they are not specified.
As you can see, nil is not a null value for each type, but only for pointers, functions, interfaces, fragments, channels and maps. This is why config == nil is an error and &config == nil not.
To check if your structure is structured, you will need to check each participant for its corresponding zero value (for example, host == "" , port == 0 , etc.) or have a private field that is set using the internal initialization method. Example:
type Config struct { Host string Port float64 setup bool } func NewConfig(host string, port float64) *Config { return &Config{host, port, true} } func (c *Config) Initialized() bool { return c != nil && c.setup }
nemo Nov 27 '13 at 14:24 2013-11-27 14:24
source share