This appendix explains Go’s concept of zero values: the default values automatically assigned to variables that are declared without explicit initialization. Zero values allow you to declare variables for later use (for example, to define scope outside a conditional) while ensuring they hold well-defined, type-appropriate defaults. The rule applies recursively to composite types, so each element or field is initialized to its own zero value, which helps avoid unpredictable behavior and reduces boilerplate initialization.
It highlights the zero values across Go’s type system: numbers default to 0, booleans to false, and strings to the empty string; arrays and structs are populated with element- or field-wise zero values. By contrast, reference-like types—slices, maps, pointers, interfaces, channels, and function types—default to nil. Slices and maps deserve special attention: although a nil slice has length 0 and can be safely appended to (yielding a non-nil slice), a nil map cannot accept writes and will panic on assignment; however, reading from a nil map returns the element type’s zero value and a false “found” flag. In practice, it’s usually better to check length than nil-ness, and to allocate slices and maps with make when you intend to write to them.
The appendix also shows how to leverage zero values to simplify code, such as counting words with a map from string to int. Because a missing key yields 0, you can increment counters unconditionally without branching, letting the language’s defaults handle first encounters. More broadly, embracing zero values leads to clearer control flow, fewer explicit initializations, and safer operations: declare variables confidently, check lengths before indexing, read from maps defensively, and allocate with make when mutation is required.
FAQ
What is a zero value in Go?It’s the default value a variable of any type gets when declared without an explicit initializer (var x T). For example, an int is 0. This lets you declare variables before use, such as outside a conditional, and still use them safely.What are the common zero values by type?Numbers: 0 (integers), 0.0 (floats), 0+0i (complex); rune: 0; bool: false; string: ""; pointers: nil; slices: nil; maps: nil; channels: nil; function types: nil; interfaces: nil; arrays: all elements set to their zero values; structs: each field set to its type’s zero value.Do zero values apply recursively to composite types?Yes. Arrays and structs are fully initialized: every element or field gets its type’s zero value. Slices and maps themselves are nil until initialized, but their elements or entry values (once present) are zero-initialized by type.What is the zero value of slices and maps, and what does nil mean here?Both slices and maps have a zero value of nil. A nil slice has no underlying array; a nil map has no backing hash table. You must initialize them (typically with make) before writing elements.What’s the difference between a nil slice and an empty slice?A nil slice is equal to nil; an empty slice is non-nil but has length 0. For both, len returns 0. Indexing either at data[0] panics if length is 0. Appending to a nil slice is allowed and produces a non-nil slice.Is it safe to call len on nil slices or maps?Yes. len(nilSlice) and len(nilMap) both return 0. In most cases, checking length is preferable to checking whether the variable is nil.Can you append to a nil slice?Yes. Appending to a nil slice does not panic; it returns a non-nil slice containing the appended elements.Can you write to a nil map?No. Assigning to a nil map panics (assignment to entry in nil map). Initialize the map first, e.g., m := make(map[K]V).Can you read from a nil map safely?Yes. Reading returns the element type’s zero value, and with the “comma ok” form, ok is false. It’s often safest to check the map’s length before relying on contents.How can zero values simplify code, such as counting occurrences?Accessing a missing key in a map[string]int returns 0, so you can increment counters directly (wordCounter[word]++) without checking if the key exists first.
pro $24.99 per month
access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!