In Go, to print the memory address of a variable, struct, array, slice, map, or any other structure, you need to generate a pointer to the value with the address operator &
and use the fmt.Println()
function (or any other print function from the fmt
package) to write the value address to the standard output. If you want to put the address as part of the text, you can use the %p
format verb in the fmt.Printf()
function.
Example 1: Print the address of a variable
// variable
i := 32
fmt.Println(&i)
// pointer to the variable
p := &i
fmt.Println(p)
Output:
0xc00001c088
0xc00001c088
Example 2: Format the address of a variable
// variable
i := 32
fmt.Printf("Address of i=%d:\t%p\n", i, &i)
// pointer to the variable
p := &i
fmt.Printf("Address of p=&i=%d:\t%p\n", *p, p)
Output:
Address of i=32: 0xc00001c088
Address of p=&i=32: 0xc00001c088
Example 3: Format addresses of various Go data structures: array, slice, struct, struct field, and map
package main
import "fmt"
type A struct {
Number int
Text string
}
func main() {
// array
arr := [3]int{1, 2, 3}
fmt.Printf("Address of array = %v: %p\n", arr, &arr)
// slice
slice := []int{1, 2, 3}
fmt.Printf("Address of slice = %v: %p\n", slice, &slice)
// struct
structInstance := A{Number: 23, Text: "abc"}
fmt.Printf("Address of struct = %+v: %p\n", structInstance, &structInstance)
// struct field
fmt.Printf("Address of struct field = %s: %p\n", structInstance.Text, &structInstance.Text)
// map
mapInstance := map[int]int{
0: 1,
}
fmt.Printf("Address of map = %v: %p\n", mapInstance, &mapInstance)
}
Output:
Address of array = [1 2 3]: 0xc0000b4000
Address of slice = [1 2 3]: 0xc0000a4018
Address of struct = {Number:23 Text:abc}: 0xc0000a4048
Address of struct field = abc: 0xc0000a4050
Address of map = map[0:1]: 0xc0000aa020
As you may know, map index expressions are not addressable, so it is not possible to print the address of a map value with a specific key.
Print the address of a pointer
In Example 1 and Example 2, we created a new pointer p
of type *int
and assigned to it the address of variable i
. Then, we printed the value of the pointer p
with fmt.Println(p)
, which is the address of the variable i
. Since the pointer itself is a value, we can also print the address of that value using fmt.Println(&p)
. Compare outputs of printing the address of the variable i
, the value of p
, and the pointer of p
:
// get the address of a variable
i := 32
fmt.Printf("Address of i=%d: %p\n", i, &i)
// get the address the pointer points to
p := &i
fmt.Printf("Address of p=&i=%d: %p\n", *p, p)
// get the address of pointer
fmt.Printf("Address of pointer p=&i=%d: %p\n", *p, &p)
Output:
Address of i=32: 0xc00001c088
Address of p=&i=32: 0xc00001c088
Address of pointer p=&i=32: 0xc00000e030
The diagram below shows the relationship between variables and pointers in memory.
Print the address of a slice underlying array
Slices are values, so to print the address of a slice, you need to use the address operator &
, just as you would for normal variables. But the verb %p
also has a special meaning for slices. Quoting the documentation of the fmt
package:
%p address of 0th element in base 16 notation, with leading 0x
To print the address of the 0th element of the slice, which is equivalent to the address of the slice underlying array, you just need to use the %p
format with the slice value argument. See the code below to compare the address of an example array, the address of a slice created by “slicing” the array, and the address of the 0th element of the slice (the address of the underlying array).
// print address of array
arr := [3]int{1, 2, 3}
fmt.Printf("Address of arr: %p\n", &arr)
// print address of slice
// "slicing" the array
slice := arr[:]
fmt.Printf("Address of slice: %p\n", &slice)
// print address of the slice underlying array
fmt.Printf("Address of the slice underlying array: %p\n", slice)
// print address of 0th element of the slice
fmt.Printf("Address of 0th element of the slice: %p\n", &slice[0])
Output:
Address of arr: 0xc000014120
Address of slice: 0xc00000c030
Address of the slice underlying array: 0xc000014120
Address of 0th element of the slice: 0xc000014120
Print an address without leading 0x
To print the address without leading 0x, use the formatting pointer verb with the #
flag: %#p
.
// print address of array
arr := [3]int{1, 2, 3}
fmt.Printf("Address of arr: %#p\n", &arr)
Output:
Address of arr: c000014120
Assign an address to a string
To get the address of a variable and assign it to string, use fmt.Sprintf()
function:
arr := [3]int{1, 2, 3}
addr := fmt.Sprintf("%p", &arr)
fmt.Println(addr)
Output:
0xc0000b4000