Go - Zero Value



In Go, the Zero Value is a default value assigned to a variable when it is declared but not initialized. Each type has its own zero value, ensuring variables always hold a valid value, even without explicit initialization.

The following are the Common Data Types for Zero Value −

S.No Type Zero Value
1 Numeric types 0
2 Boolean false
3 String "" (empty string)
4 Pointers nil
5 Slices nil
6 Maps nil
7 Channels nil
8 Functions nil
9 Interfaces nil
10 Structs All fields set to their zero values

How Zero Value works Using different types?

Let's understand how zero value works using different types through example −

Example

In this example, we demonstrate zero values by declaring variables of different types (int, float64, bool, string, pointer, slice, and map) without initializing them. then it prints their default (zero) values, showing what Go assigns when no value is provided.

package main
import "fmt"
func main() {
   var i int       
   var f float64   
   var b bool      
   var s string    
   var p *int     
   var slice []int
   var m map[string]int
   
   fmt.Println("int:", i)
   fmt.Println("float64:", f)
   fmt.Println("bool:", b)
   fmt.Println("string:", s)
   fmt.Println("pointer:", p)
   fmt.Println("slice:", slice)
   fmt.Println("map:", m)
}

Following is the output to the above program −

int: 0
float64: 0
bool: false
string: 
pointer: <nil>
slice: []
map: map[]

How Zero Value works Using Struct

Let's understand how Zero Value works Using Struct in Golang.

Example

In this example, we define a Person struct with fields 'Name' and 'Age'. In the main function, a variable 'p' of type 'Person' is declared but left uninitialized, so its field take their zero values ("" for Name and 0 for Age) and prints the default values of p.

package main
import "fmt"
type Person struct {
    Name string
    Age  int
}
func main() {
    var p Person 
    fmt.Println("Person:", p)
}

Following is the output to the above program −

Person: { 0}

How Zero Value works Using Function

A function works with zero values by returning the default value assigned to a variable when it is declared but not initialized.

package main
import "fmt"
func getZeroInt() int {
   var i int
   return i
}
func getZeroBool() bool {
   var b bool
   return b
}
func main() {
   fmt.Println("Zero value for int:", getZeroInt())
   fmt.Println("Zero value for bool:", getZeroBool())
}

Following is the output to the above program −

Zero value for int: 0
Zero value for bool: false

How Zero Value Works Using Interfaces

An interface is a type that specifies a set of method signatures. To this interface we have declared a variable but not initialized. So, its zero value is nil that means the interface does not hold any concrete value or type.

Example

In this Example, we define an interface Speaker with a method Speak. In the main function, a variable i of type Speaker is declared but not initialized. So, it holds the zero value for interfaces which is nil.

package main
import "fmt"
type Speaker interface {
   Speak() string
}
func main() {
   var i Speaker
   fmt.Println("Interface:", i)
}

Following is the output to the above program −

Interface: <nil>

How Zero Value Works Using Channels

In Go, a channel is a connection through which we can send and receive values. When a channel variable is declared but not initialized, its zero value is nil. A nil channel is not used for sending or receiving values until it is initialized.

Example

In this example, we declare a channel variable as 'ch' of type 'chan int' but we will not initialize it. As ch is not initialized, it takes the zero value for channels which is nil.

package main
import "fmt"
func main() {
   var ch chan int
   fmt.Println("Channel:", ch)
}

Following is the output to the above program −

Channel: <nil>
Advertisements