Go Reference ยท Concepts
Go zero values
Reference for Go zero values: what every type is initialized to, nil maps, slices and pointers, zero-value structs, and designing types whose zero value works.
Every variable in Go has a value from the moment it exists. If you do not give one, the variable holds the zero value of its type: 0 for numbers, false for booleans, "" for strings and nil for pointers, functions, interfaces, slices, maps and channels. This is guaranteed by the language, not a convention. There is no such thing as uninitialized memory in safe Go code.
Zero values apply everywhere something is allocated: var x T, new(T), make (for the elements), the fields you leave out of a composite literal, array elements, and the result variables of a function. Good Go design uses this: a type is easy to use when its zero value is already a valid, ready-to-use value.
var n int // 0 var s string // "" var p *int // nil var m map[string]int // nil
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
There is no special syntax; this table shows the value each type gets.
| Type | Zero value |
|---|---|
bool |
false |
int, uint, int8...uint64, uintptr, byte, rune |
0 |
float32, float64 |
0 |
complex64, complex128 |
(0+0i) |
string |
"" (empty string, never nil) |
Pointer, unsafe.Pointer |
nil |
func |
nil |
Interface (including error and any) |
nil (no dynamic type, no value) |
| Slice | nil (length 0, capacity 0) |
| Map | nil (reads work, writes panic) |
| Channel | nil (send and receive block forever) |
Array [N]T |
N zero values of T |
| Struct | every field set to its own zero value |
Errors and edge cases: assigning to a nil map panics at run time with assignment to entry in nil map. Closing or sending on a nil channel blocks (send) or panics (close). Calling a nil function panics with a nil pointer dereference. Methods called through a nil pointer work only if the method does not touch the receiver's fields.
Usage notes
new(T)returns a pointer to the zero value.new(int)points at0, and&Point{}is the same asnew(Point).- A nil slice is a perfectly good empty slice.
len,cap,rangeandappendaccept it. Only encoding differs:encoding/jsonwrites a nil slice asnulland an empty non-nil slice as[]. - A nil map is read-only. Always create maps with
makeor a literal before storing in them. This is the most common zero-value bug. - Struct comparison uses zero values.
p == Point{}is the cheap test for "nothing set", provided all fields are comparable. - Zero is not always "unset". For JSON, config and database code,
0and "missing" are different facts. Use a pointer,sql.NullString-style types or a separate flag when the difference matters. - Make the zero value useful.
sync.Mutex,sync.WaitGroup,bytes.Bufferandstrings.Builderall work without a constructor. Do not copy such a value after first use, because its state lives inside it. - Declaring is initializing.
var x intdoes not read garbage; it costs the same asx := 0. - A nil interface is not an interface holding a nil pointer. See Nil interface pitfall.
Related
- Slice aliasing and append, Nil interface pitfall, new(), make(), map
- Lessons: Variables, Structs, Maps