Variable with zero value

Part of Golang Mastery course

~15 min read
Interactive
Hands-on
Beginner-friendly

Variables with Zero Values#

In Go, variables declared without an explicit initial value are given their zero value. The zero value depends on the type of the variable.

example.go
package main
 
import string">"fmt"
 
func main() {
var a int
var b string
var c float64
var d bool
 
fmt.Printf(string">"%v \n", a)
fmt.Printf(string">"%v \n", b)
fmt.Printf(string">"%v \n", c)
fmt.Printf(string">"%v \n", d)
 
fmt.Println()
}
 

Run in Go Playground →

Output:#

0 0 false

In the program above, we declared variables with their types but didn't assign any values. Go automatically assigns zero values:

  • For integers (int): 0
  • For strings (string): empty string
  • For floating-point numbers (float64): 0.0
  • For booleans (bool): false

Your Progress

9 of 103 modules
9%
Started9% Complete
Previous
SpaceComplete
Next