Last modified: Jan 22, 2026 By Alexander Williams
Go Default Values for Uninitialized Variables
Go is a statically typed language. It has a unique feature. Variables declared without an explicit initial value get a default. This default is called a "zero value".
Understanding zero values is crucial. It prevents bugs. It makes your code predictable. This guide explains all default values in Go.
What Are Zero Values?
A zero value is the default value. Go assigns it to a variable. This happens when you declare a variable without giving it a value.
Every type in Go has a zero value. It is not random. It is a defined, predictable state. This ensures memory safety.
You can declare variables without value in Go confidently. You know they will hold a valid state.
Zero Values for Basic Types
Let's look at the zero values for Go's basic built-in types.
Numeric Types
All numeric types default to 0 (or 0.0). This includes integers, floats, and complexes.
package main
import "fmt"
func main() {
var i int
var f float64
var c complex128
fmt.Println("int zero value:", i) // 0
fmt.Println("float64 zero value:", f) // 0
fmt.Println("complex128 zero value:", c)// (0+0i)
}
int zero value: 0
float64 zero value: 0
complex128 zero value: (0+0i)
Boolean Type
The bool type has a zero value of false.
var ready bool
fmt.Println("bool zero value:", ready) // false
String Type
The string type has a zero value of an empty string ("").
var name string
fmt.Printf("string zero value: '%s'\n", name) // ''
This is helpful when building a Go list of strings where elements might be empty.
Zero Values for Composite Types
Composite types also have sensible zero values.
Arrays
All elements of an array are set to their respective zero values.
var arr [3]int
fmt.Println("array zero values:", arr) // [0 0 0]
Slices, Maps, and Channels
These types are references. Their zero value is nil. A nil slice or map has length 0.
var s []int
var m map[string]int
var ch chan int
fmt.Println("slice is nil:", s == nil) // true
fmt.Println("map is nil:", m == nil) // true
fmt.Println("channel is nil:", ch == nil)// true
You can declare multiple variables in one line in Go, all receiving their zero values.
Pointers, Functions, and Interfaces
These also default to nil. A nil interface holds neither value nor concrete type.
var ptr *int
var fn func()
var iface interface{}
fmt.Println("pointer is nil:", ptr == nil) // true
fmt.Println("function is nil:", fn == nil) // true
fmt.Println("interface is nil:", iface == nil) // true
Structs
A struct's zero value is a struct where all fields are set to their zero values.
type Person struct {
Name string
Age int
}
var p Person
fmt.Printf("struct zero values: %+v\n", p) // {Name: Age:0}
Why Zero Values Matter
Zero values provide safety. Your program never uses uninitialized, random memory. This prevents undefined behavior.
They enable simpler code. You can check if a map is nil before using it. You can append to a nil slice safely.
They work well with Go's essential Go tooling. Tools like the compiler and vet rely on this predictability.
Checking for Zero Values
You often need to check if a variable holds its zero value. Use a simple comparison.
var count int
var text string
if count == 0 {
fmt.Println("count is zero")
}
if text == "" {
fmt.Println("text is empty")
}
var data []byte
if data == nil {
fmt.Println("slice is nil, initializing")
data = make([]byte, 10)
}
Use the fmt package to print variable values in Go for debugging these states.
Common Pitfalls and Tips
Nil Maps: A nil map cannot hold keys. You must initialize it with make before use.
var scores map[string]int
// scores["Alice"] = 95 // PANIC: assignment to entry in nil map
scores = make(map[string]int) // Correct initialization
scores["Alice"] = 95
Nil Slices are Usable: You can call len and cap on a nil slice. You can also append to it.
var items []string
fmt.Println(len(items)) // 0, no panic
items = append(items, "hello") // Works fine
Choosing Good Go variable naming rules helps. A name like userCount implies a number where 0 is meaningful.
Conclusion
Go's zero values are a foundational concept. They provide safety, simplicity, and predictability.
Every type has a defined default state. Numeric types are 0. Booleans are false. Strings are empty. Reference types are nil.
Use this knowledge to write robust code. Always consider the zero state when declaring variables. Check for it when logic depends on initialization.
Embrace zero values. They are a key part of Go's philosophy of simplicity and clarity.