Go Zero Value – Understand Default Values of Variables in Go
Introduction – What Is a Zero Value in Go?
In Go, when you declare a variable without explicitly initializing it, the variable is automatically assigned a zero value. This concept ensures safety and predictability, avoiding undefined or garbage data — a common issue in low-level languages like C or C++.
In this section, you’ll learn:
- What zero values are and why they exist
- Zero values of different Go data types
- How to check and use zero values in real programs
- Practical examples and best practices
What Is the Zero Value?
A zero value is the default value given to a variable when it is declared but not explicitly initialized.
Example:
var x int
fmt.Println(x) // Output: 0
Even though x isn’t initialized, Go ensures it holds a known, safe value (0 for int).
Zero Values by Data Type
| Data Type | Zero Value | Example Output |
|---|---|---|
int, int32 | 0 | 0 |
float64 | 0.0 | 0 |
bool | false | false |
string | "" (empty) | "" |
pointer | nil | <nil> |
interface{} | nil | <nil> |
slice | nil | <nil> |
map | nil | <nil> |
channel | nil | <nil> |
function | nil | <nil> |
struct | All fields zeroed | Depends on field types |
Example – Zero Values in Action
package main
import "fmt"
type User struct {
Name string
Age int
Paid bool
}
func main() {
var u User
fmt.Println(u) // Output: { 0 false}
fmt.Println(u.Name) // ""
fmt.Println(u.Age) // 0
fmt.Println(u.Paid) // false
var ptr *int
fmt.Println(ptr) // <nil>
var prices []float64
fmt.Println(prices) // []
var done bool
fmt.Println(done) // false
}
Why Zero Values Matter
Safe Defaults – You don’t need to initialize every variable just to avoid compiler errors.
Clear Semantics – Every type has a predictable zero value.
Idiomatic Go – Many Go developers rely on zero values as the initial state of structs and variables.
Common Use Cases
Struct Initialization Without Values
type Config struct {
Port int
Enabled bool
}
var cfg Config
// cfg.Port == 0, cfg.Enabled == false
Slice and Map Defaults
var names []string // names == nil
var scores map[string]int // scores == nil
You must initialize slices/maps before using them:
names = append(names, "Alice") //
scores = make(map[string]int) //
Summary – Recap & Next Steps
Zero values make Go safe and predictable. Whether you’re declaring variables, defining structs, or handling collections, you can trust Go to set an appropriate starting value.
Key Takeaways:
- Every Go variable has a zero value if uninitialized
0,false,"", andnilare common zero values- Use zero values when building structs or checking states
- Always initialize slices, maps, and channels before use
Next: Learn how to import packages, including standard and third-party libraries, using Go’s import keyword.
FAQs – Go Zero Values
What is the default value of an uninitialized variable in Go?
The variable takes a zero value, depending on its type. For instance, int is 0, string is "", bool is false.
Are slices and maps automatically initialized in Go?
No. They default to nil and must be initialized using make() before use.
Can zero values be used as default struct states?
Yes. Structs automatically assign zero values to each field when declared.
Is nil the zero value for all reference types?
Yes. Pointers, maps, slices, functions, channels, and interfaces default to nil.
Is the zero value always falsey in Go?
Not always. Go doesn’t have truthy/falsy rules like JavaScript. Only booleans can be directly tested in if conditions.
Share Now :
