...

/

Understanding Go Pointers

Understanding Go Pointers

Understand Go Pointers, their functionality, and when to use them.

Introduction

Pointers are another essential tool for programming languages for efficient memory use. Some readers may not have encountered pointers in their current language, instead having used its cousin, the reference type. In Python, for example, the dict, list, and object types are reference types. The illustration below depicts a variable x, and the address it’s stored in.

Press + to interact
The address and assigned value of x
The address and assigned value of x

In this lesson, we'll cover what pointers are, how to declare them, and how to use them.

Memory addresses

In an earlier lesson, we talked about variables for storing data of some type. For example, if we want to create a variable called x that stores an int type with a value of 23, we can write var x int = 23.

Under the hood, the memory allocator allocates space to store the value. The space is referenced by a unique memory address that looks like 0xc000122020. This is similar to how a home address is used; it is the reference to where the data lives. We can see the memory address ...