Go Pointers Tutorial

In this Go tutorial we learn how to find data in a system's memory with pointers.

We learn how to find a value's address in memory with the reference of operator, and how to retrieve the value from that address with the dereference operator.

Lastly, we learn how to declare or initialize a pointer.

What is a pointer

Simply put, a pointer is a variable that stores the memory address of another variable.

When an application runs, it stores its temporary data, like variables, in a random location in the system’s memory.

When the data isn’t used anymore, or when the program quits, the data is removed from memory and the space it reserved becomes available to the system again.

As an example, think of a gated community. John has invited us to visit him and gave us the location of the gated community where he lives.

We know the general location of where he lives. Similarly, we only know the general location of temporary data, memory.

We can go to the gated community, but if we want to visit John, we would need to know his specific house address.

When we arrive at the security gate, we have to ask the guard in which house our friend John lives. The guard has a list of stored house addresses and can quickly find the one we want.

Similarly, if we want the address of a piece of temporary data, we need to find out where it is and store it.

Pointers allow us to do just that. They point us to the address of our data in memory, much like the guard would point us to John’s house in the community.

To use pointers, we need three things:

  • A pointer variable that stores the pointer.
  • The address of the pointer.
  • A way to access the value at the address.

How to get the address of a variable with the reference of operator ( & )

We use the unary reference of ( & ) operator to get the memory address of a variable. The reference operator is also known as the address of operator.

For example, if we had a variable called num, we would use &num to get its address in memory.

Example:
package main

import "fmt"

func main() {

    num := 3

    fmt.Println("Variable value:", num)
    fmt.Println("Variable address:", &num)
}

In the example above, we use the unary & operator immediately in front of the variable name to get its address.

Output:
Variable value: 3
Variable address: 0xc00000a0a0

The num variable is stored at the address 0xc00000a0a0. The variable name num is just the name we give to that location.

The address of the variable may be different on your system.

How to get the value stored in a memory address with the dereferencing operator ( * )

Now that we have the memory address of the variable, we can retrieve the value stored in that address.

To do this, we need to dereference the memory address with the unary dereferencing operator ( * ). The dereference operator is also known as the indirection operator.

Example:
package main

import "fmt"

func main() {

    num := 3
    numAD := &num

    fmt.Println("Variable value:", num)
    fmt.Println("Variable address:", numAD)
    fmt.Println("Value at address:", *numAD)
}

In the example above, we store the memory address in the numAD variable. Then we dereference numAD with *numAD to get the value stored at its address.

Output:
Variable value: 3
Variable address: 0xc00000a0a0
Value at address: 3

As expected, the value is 3.

How to declare/initialize a pointer

The structure of a pointer variable is similar to that of a regular variable.

Because a pointer is a variable, it needs to have a valid Go data type. We also prefix the pointer type with the * operator.

It’s important to note that the ____ operator’s usage here is not the dereferencing operator, just similar notation.*

Syntax:
 var name *type
Example:
package main

import "fmt"

func main() {

    // variable init
    var num int = 8
    // pointer declaration
    var ptr *int

    // store address of num in ptr
    ptr = &num
    fmt.Println("ptr (address of num):", ptr)

    // dereference ptr to get the value
    // stored at its address
    fmt.Println("ptr (value of num):", *ptr)
}

In the example above, we store the address of num in a pointer called ptr. Then we dereference the pointer to get the value at its address.

Output:
ptr (address of num): 0xc00000a0a0
ptr (value of num): 8

Summary: Points to remember

  • A pointer is a variable that stores the address in memory of another variable.
  • We use the reference of operator ( & ) to get the address of a variable.
  • We use the dereference operator ( * ) to get the value at an address.
  • A pointer is declared like a variable, except we prefix the type with the * operator.
  • A pointer that’s declared instead of initialized with an address, will have a nil value.