zero value
The value Go gives a variable automatically when you declare it without one: 0 for numbers, "" for strings, false for booleans and nil for pointers, slices and maps.
In more detail
Because the zero value is always defined, a fresh struct is valid rather than full of random data. Good Go design makes the zero value mean the safe default. The catch is that you cannot tell a value that was never set from one deliberately set to zero; use a pointer field or an extra boolean when that difference matters.
Example
var l limits
fmt.Printf("%+v\n", l) // {Retries:0 Timeout: Strict:false}Related terms
- var declaration — The Go statement that declares a variable with the var keyword, optionally giving its type and a starting value.
- nil — The Go value meaning nothing or not set, which is the zero value for pointers, slices, maps, channels, functions, interfaces and errors.
- struct — A Go type that groups named fields of different types into one value, such as a Target with a Name and a Port.
- pointer — A value that stores the memory location of another piece of data, so the program can follow it to that data.
Taught in
- Your First Program and go run — Concurrent Systems Programming with Go
- Variables, Typing, and Zero Values — Concurrent Systems Programming with Go
- Pointers and Memory — Concurrent Systems Programming with Go
- Arrays, Slices, and Maps — Concurrent Systems Programming with Go
- Structs and Methods — Concurrent Systems Programming with Go
- Errors as Values — Concurrent Systems Programming with Go