Go

Go Basics - 10. Pointers in Go

Updated:

In this article, we are going to explore how pointers work in the Go programming language.

package main

import (
	"fmt"
)

func main() {

	a := 14
	b := a

	fmt.Println(a, b)
}

/*
  The result of the code above is:
  14 14
*/

In this first example, we declare the variable a to be an int of 14. Then we assign the variable b to be the value of a. Since Go is passing by value and not by reference in this example, the variable b is a copy of a.

We can prove this with the following example.

package main

import (
	"fmt"
)

func main() {

	a := 14
	b := a

	fmt.Println(a, b)

	a = 55

	fmt.Println(a, b)
}

/*
  The result of the code above is:
  14 14
  55 14
*/

Pointers

We can use pointers to make the b variable 'point' to the value of a.

package main

import (
	"fmt"
)

func main() {

	var a int = 14
	var b *int = &a

	fmt.Println(a, b)
}

/*
  The result of the code above is:
  14 0x40e020
*/

The variable b now holds the location in memory where the value of the variable a is stored. So, the variable b now 'points' to the same location in memory as the variable a.

Let's break down this new syntax:

var b *int = &a

The * symbol is a pointer, and the & gives the memory address of the variable that comes after it. So in this example we are saying that variable b is a pointer to an int and it is equal to the same location in memory as the variable a.

Dereferencing pointers

package main

import (
	"fmt"
)

func main() {

	var a int = 14
	var b *int = &a

	fmt.Println(a, *b)
}

/*
  The result of the code above is:
  14 14
*/

Notice in this example that the only change made from the previous example is this line:

fmt.Println(a, *b)

Putting the * in front of the variable b is called dereferencing. This might seem a little confusing because in this line

var b *int = &a

the * is a pointer. However, when you then use the same * in front of a pointer, it is called dereferencing, which means give me the value of this pointer.

So to reiterate this example one more time:

var b *int = &a

The * symbol is a pointer, and the & gives the memory address of the variable that comes after it. So in this example we are saying that variable b is a pointer to an int and it is equal to the same location in memory as the variable a.

fmt.Println(a, *b)

The * symbol, in this case, says dereference the pointer b and give me the value that it is 'pointing' to.

So if we modify our earlier example using pointers and dereference we see that by re-declaring the variable a changes the value of the variable b since b is 'pointing' to the same value as a.

package main

import (
	"fmt"
)

func main() {

	var a int = 14
	var b *int = &a

	fmt.Println(a, *b)

	a = 55

	fmt.Println(a, *b)
}

/*
  The result of the code above is:
  14 14
  55 55
*/

Wrap Up

In this article, we learned how to create and work with pointers in Go.

Additional Resources

Previous
9. Loops