Home »
Golang »
Golang FAQ
What is the default value of a map in Go language?
Here, we will learn about the map variable, and the default value of a map in Golang.
Submitted by IncludeHelp, on October 03, 2021
A map is a collection of unordered pairs of key-value. The default value of a map variable in the Go programming language is nil. The length of a nil map is o. But if you print the nil map – the output will be "map[]".
Example 1:
// Golang program to demonstrate the
// default value of a map
package main
import "fmt"
func main() {
var x map[string]interface{}
fmt.Println("Default value of a map is", x)
fmt.Println("Length of an empty map is", len(x))
}
Output:
Default value of a map is map[]
Length of an empty map is 0
Example 2:
// Golang program to demonstrate the
// default values of maps
package main
import "fmt"
func main() {
var x map[int]interface{}
var y map[float64]interface{}
var z map[string]interface{}
// Printing the types & default values
fmt.Printf("x: %T, %v\n", x, x)
fmt.Printf("y: %T, %v\n", y, y)
fmt.Printf("z: %T, %v\n", z, z)
}
Output:
x: map[int]interface {}, map[]
y: map[float64]interface {}, map[]
z: map[string]interface {}, map[]
Golang FAQ »