What is the default value of an empty interface in Go language?

Here, we will learn about the empty interface and the default value of an empty interface in Golang.
Submitted by IncludeHelp, on October 03, 2021

An interface is a custom type that is used to specify a set of one or more method signatures. The default value of an empty interface in the Go programming language is nil.

Example 1:

// Golang program to demonstrate the
// default value of an empty interface

package main

import "fmt"

func main() {
	var x interface{}

	fmt.Println("Default value of an empty interface is", x)
}

Output:

Default value of an empty interface is <nil>

Example 2:

package main

import "fmt"

// Function to print type & value
// of the inteface
func PrintTypeAndValue(x interface{}) {
	fmt.Printf("(%v, %T)\n", x, x)
}

func main() {
	var i interface{}
	PrintTypeAndValue(i)

	i = 108
	PrintTypeAndValue(i)

	i = "Shri Ram"
	PrintTypeAndValue(i)
}

Output:

(<nil>, <nil>)
(108, int)
(Shri Ram, string)

Golang FAQ »



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.