Golang: Zero Value

By Xah Lee. Date: . Last updated: .

Variables declared without an explicit initial value are given their default value.

Default value are different for different types of variables.

The default value for a type is called its zero value.

The zero values are:

0
for numeric types
false
for boolean type.
""
(empty string) for string type.
nil
for pointers, functions, interfaces, slices, channels, and maps.
package main

import "fmt"

func main() {

	var i int
	var f float64
	var b bool
	var str string
	var sl []int

	fmt.Println(i == 0)
	fmt.Println(f == 0)
	fmt.Println(b == false)
	fmt.Println(str == "")
	fmt.Println(sl == nil)

}

Reference

Golang Spec #The_zero_value

Golang

Compile and Run

String

Types and Values

Branching and Loop

Data Structure

Function

Misc

Examples

Reference