In Go, a pointer is a variable that stores the address of another variable. We can create a pointer to a pointer, also known as a double pointer, by declaring another pointer variable that stores the address of the original pointer variable.
Here's an example of how to create a pointer to a pointer in Go:
main.go428 chars12 lines
In this example, we first declare an integer variable x
with the value of 10. We then declare a pointer to x
called p1
by using the &
operator to get the address of x
.
Next, we declare a pointer to a pointer to x
called p2
with the **
notation. We set p2
to the address of p1
by using the &
operator again.
Finally, we print out the values of x
, p1
, and p2
. We use the *
operator to dereference p1
and **
to dereference p2
twice. Dereferencing p1
gives us the value of x
, and dereferencing p2
twice also gives us the value of x
.
Output:
main.go47 chars4 lines
gistlibby LogSnag