×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang program to print the variable address using '%p' format specifier

Here, we are going to learn how to print the variable address using '%p' format specifier in Golang (Go Language)?
Submitted by Nidhi, on April 18, 2021 [Last updated : March 02, 2023]

How to print the variable address in Golang?

Problem Solution:

In this program, we will create an integer number and assign the address of the variable to the pointer and then we will print the address of the variable using the "%p" format specifier on the console screen.

Program/Source Code:

The source code to print the variable address using the "%p" format specifier is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

Golang code to print the string in double-quotes using format specifier

// Golang program to print the variable address
// using "%p" format specifier

package main

import "fmt"

func main() {
	var num int = 20
	var ptr *int

	ptr = &num

	fmt.Printf("Value   : %d\n", *ptr)
	fmt.Printf("Address : %p\n", &num)
	fmt.Printf("Address : %p\n", ptr)
}

Output:

Value   : 20
Address : 0xc000100010
Address : 0xc000100010

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package then we can use a function related to the fmt

In the main() function, we created an integer variable num and a pointer ptr. After that, we assigned the address of variable num to the pointer ptr and then we printed the address of the variable using the "%p" format specifier on the console screen.

Golang Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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