When Should You Use Pointers in Golang

Pavle Djuric
6 min readJun 23, 2022

The concept of pointers in programming languages is quite old. C and C++ are probably the best examples of languages that are widely popular that use pointers. Golang, being inspired by C in many ways, implements explicit pointers as well. I say explicit, because the most popular programming languages like Python and JavaScript implement pointers also, except they do them implicitly so you don’t even know about this.

Ever hear of the term “pass by reference” ? It’s used all the time in Python, JS and many more. Here’s an example in Python to describe what I mean:

a = [1,2,3]
b = a
b.append(4)
print(a)

You’ll see from this simple example that not only are the values of a and b the same, but a and b are the same. You can check this by printing the memory addresses of both:

print(id(a))
print(id(b))

The reason they are the same is because when we say b=a, we're not saying assign b the value of a, rather assign b to be a pointer to a.

Languages like Python and JavaScript were created to abstract away many low level concepts so that developers had less on their mind, and could focus more on business logic. These languages were created in the 1990s when memory was scarce. Why is that important for pointers? Well, imagine you have a data structure like this:

--

--

Pavle Djuric
Pavle Djuric

Written by Pavle Djuric

Software developer. 3 x AWS certified. Writing mostly about Python, Golang, backend development and Cloud computing.