Zero values
The zero value is what a variable holds when you declare it and do not assign anything. Every type has one: 0, false, "", nil, or a struct whose fields are themselves zero.
Detailed Explanation
In many languages an uninitialized local is either forbidden or garbage. Go always initializes. var n int is 0. var s string is "". var p *int is nil. You never read leftover stack contents. That rule is why Go code can say var buf bytes.Buffer and start writing: the zero Buffer is a usable empty buffer.
The zeros by kind.
| Kind | Zero |
|---|---|
| Integers, floats | 0 |
bool | false |
string | "" (empty, not nil) |
| Pointers, slices, maps, channels, functions, interfaces | nil |
| Array | each element zero |
| Struct | each field zero |
A string's zero is a real string of length 0. A slice's zero is nil, which has length 0 and capacity 0 and is safe to range over, but is not the same as []T{} when you compare with == nil. A map's zero is nil: reads return the value type's zero, writes panic.
Design around the zero. A useful Go type makes the zero value ready to use, or clearly unusable. sync.Mutex is ready. bytes.Buffer is ready. A net.Conn that is a zero struct is not a connection — you document that callers must go through Dial. When you write a struct, ask what var s T should mean. If it should mean "empty session," make empty fields do the right thing. If it cannot, hide the type behind NewT() and keep fields unexported.
Interfaces and nil. An interface is zero only when both its type word and its value word are empty. A nil pointer stored in an interface is not a nil interface. Returning var e *MyError as error produces a non-nil error. This is the typed-nil trap, and it is the most expensive zero-value surprise in the language.
What it costs. Zeroing has a real cost on large allocations: the runtime clears the memory, which is why a 100 MiB make([]byte, 1<<27) is not free even before you write to it. For ordinary locals the cost is lost in the noise, and the safety is the point.
Code Examples
package main
import "fmt"
func main() {
var n int
var ok bool
var name string
var p *int
fmt.Println(n, ok, name == "", p == nil) // 0 false true true
var ids []int
fmt.Println(len(ids), ids == nil) // 0 true — safe to range, not to append-into-a-map-value without care
}type Counter struct {
n int
}
func (c *Counter) Inc() { c.n++ }
func main() {
var c Counter // n is 0, ready to use
c.Inc()
fmt.Println(c.n) // 1
}func demoMap() {
var m map[string]int // nil map
fmt.Println(m["missing"]) // 0, the zero for int
// m["x"] = 1 // panic: assignment to entry in nil map
m = make(map[string]int)
m["x"] = 1
}Common Gotchas
Writing to a nil map panics
reading is fine and returns the zero. Call `make` (or a literal) before the first write.
A nil concrete pointer inside an interface is not a nil interface
`var err error = (*MyError)(nil); err != nil` is true. Return a bare `nil`.
A nil slice and an empty slice compare differently to `nil`
both have length 0. JSON encodes a nil slice as `null` and an empty slice as `[]`.
`var s string` is `""`, never nil
there is no nil string in Go. Pointers to strings can be nil.