What is the default value of a pointer in Go language?

In this tutorial, we are going to learn about the pointer variable and the default value of a pointer variable in the Go language.
Submitted by IncludeHelp, on October 02, 2021

A pointer stores the memory address of a value (variable). The default value of a pointer in the Go programming language is nil.

Example:

// Golang program to demonstrate the
// default value of a pointer variable

package main

import "fmt"

func main() {
	var num *int

	fmt.Println("The default value of a pointer variable:", num)
}

Output:

The default value of a pointer variable: <nil>

Example 2: Checking whether pointer is nil or not

// Golang program to demonstrate the
// default value of a pointer variable

package main

import "fmt"

func main() {
	var num *int

	if num == nil {
		fmt.Println("Yes, the pointer is nil")
	} else {
		fmt.Println("No, the pointer is not nil")
	}
}

Output:

Yes, the pointer is nil

Golang FAQ »




Comments and Discussions!

Load comments ↻






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