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()
}
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