main banner

Development

Pointers: The GO (lang) way

Go is a programming language designed at Google (Griesemer) mainly influenced by C and C++ languages (Griesemer). In this blog, I’ll discuss the way Go faces one of the main differences between these 2 languages and high-level programming languages: pointers.

Before starting I’d like to clarify some points for this blog:

  1. Understanding programming would be helpful but it’s not necessary.

  2. This is not about Go basics.

  3. Knowledge of pointers is not necessary.

  4. I’m not going to discuss if pointers are good or bad.

  5. Let’s say C/C++ are middle-level, with assembly being low-level.

  6. I know other languages use pointers apart from C/C++ but these are in the top 5 of almost every most-used-languages index of 2021. (“Top Computer Languages 2021”)

What is a pointer?

In programming languages, we store values in variables. Depending on the language, we need to specify the type of the value being stored, although the extent varies. For example, we can store the age of a person in a number-type variable.

 

Pointers are variables that store memory addresses, most of the time these addresses point to memory locations, where other variables are stored. Even though our tasks as developers are linked to our job, most of the time, daily tasks in programming do not require pointers, thus a lot of languages do not implement them.

 

How are pointers useful?

The most common use of pointers is not to access the value being stored (remember they store memory addresses) but to access what it’s being stored at that memory location. In other words, usually, we do not use the memory address but the actual object stored at it.

 

You may be asking “why should I use a pointer to access the value of another variable if I already have the variable?” Well, you are completely right: in that scenario, it’s not needed and to understand how pointers are useful we need to know how variables are passed to functions.

 

When you pass a variable to a function there are 2 ways that the actual value can be passed:

  1. By value: This means the argument (the variable being passed) is copied to a temporary one. The function has access to the temporal one and the changes the function applies to it only stand for the scope of the function.

  2. By reference: A reference is passed to the function and the applied changes stand forever. References and pointers are very-closed concepts and sometimes one can replace the other.

 

Passing an argument by reference is, almost always, faster than by value. So, pointers are useful because, in languages where arguments are passed to functions as values by default, they let the developer decide when to pass an argument by value and when not. This gives the developer much more “control” but also implies more responsibility as it can lead to very hard-to-debug issues.

 

Pointers in high-level programming languages

Let’s say high-level languages are those that include a garbage collector and the memory management is not on the side of the developer.

 

In this kind of programming language, pointers do not even exist and usually, arguments are passed by reference to functions. This is good as these languages usually take care of memory management which removes that issue from the developer fieldside, however, they also stole the ability to pass arguments by value which, in some cases, it’s necessary and leads to inorganic instructions (this is out of the scope of this blog).

 

Go and pointers

In Go, arguments are passed to functions by value, and it’s the responsibility of the developer to instruct when they should be passed by reference. Go exposes pointers to the user for this task although in an easy-and-safe way (when comparing it with C/C++) as the user does not need to take care of memory management (it includes a garbage collector).

 

For those starting in Go and that haven’t used C/C++, it’s very important to highlight that pointers not only need to be used when you want to pass an argument by reference but also in regards to performance (just as C++ using constant references). In other words, even if a function is not modifying an argument but the argument is a complex structure you should pass it by reference (using a pointer). For example, let’s say that for some reason you’re passing an array that contains 200 objects that contain arrays and other complex objects, it’ll be much faster to pass a pointer to that array than copying.

 

Conclusion

Regarding passing arguments to functions and the things that involve, for some reason, Go designers decided to allow the user (the developer) to take that very important decision. As a person who learned programing using C/C++ and has used it for more than 9 years in complex personal projects (a game development framework, an interpreter of a basic programming language, a doc generator, etc.), I appreciate what they did because it returns to us (developers) some decisions that have been removed from our control (I’m being very dramatic, yep!) not only the ones that I mentioned but other ones that are hidden and out of the scope of this blog.

 

Works Cited

Alberto C.

I come from a small town. I like video games, playing piano, learning new things, reading, food in general, anime, movies, exercising, and cars.

Articles