- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
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>