Pointers in Golang

Learn via video courses
Topics Covered

Overview

A pointer is a special variable that stores the address of a variable. An int type pointer stores only address of an int type variable. In simple terms, instead of storing the value, it points to the variable which is storing the value.

Introduction

Golang Pointers are easy yet very powerful tools to have. Pointers were established to help and pass data around the code without having duplicate variables, which also saves memory. It is also known as a special variable in Golang. Variables are used to store data in the system at a specific memory address. Memory addresses are always in hexadecimal format (starting with 0x like 0xFFAAF etc).

Pointers in Golang

Golang pointers are variable which stores the memory address of other variables i.e., the direct address of the memory location. Some tasks such as call by reference are very handy with golang pointers as it's not possible without pointers. As a Go programmer, it's necessary to be familiar with Golang Pointers, so that you can take a variable, and place it in memory then instead of reading the actual data or value, you just get the address of where that value is stored.

  • The main problem without Golang Pointers is with the variables. Variables are the names provided to memory locations that contain actual data. To access the stored data, we require the address of the memory.
  • Golang Pointers also allows you to save a hexadecimal number into a variable using the literal expression, which means that any integer beginning with 0x is a hexadecimal number.
  • Pointer variables are useful because they are affordable. This indicates that they take up a set space regardless of the size of the value they point to. A pointer variable's size is 8 bytes for 64-bit computers and 4 bytes for 32-bit computers. Imagine a variable that stores a string that can be any size, but the pointer that points to it has a set size. This is significant because we frequently provide arguments—data—to functions. A regular variable would work if the function required a local copy of the data. However, we can transfer a variable by reference through a pointer-type variable if we want to change the value of the original variable rather than by its value. Despite the fact that we are pointing to a different function scope, the original data is still there.

The main concept includes:

  • pointer value
  • pointer address
  • pointer type

Syntax of the Declaration of Pointers in Go

pointer variable declaration is

The zero-value of the pointer is nil.

The * and & Operators

* It is used to store the memory address and also used to “dereference” pointer variables. Let's see how to assign the memory address of a variable to a pointer variable.

Initialization of pointer using address-of(&) operator. Here is the way to do it.

For Example:

Output:

Run the code!

Another Example:

Output:

In the above code, we declare the variable name with its value scaler. In the second line, we are creating a pointer variable ptr which will store the string type memory address i.e name and print the same address as output.

Run the code!

& It is used to access the memory address of the variable.

For Example:

Output:

Run the code!

In the above code, we printed num and name variables by declaring their values i.e 20 and scaler and tried to observe that we are using the address operator & for accessing the address of a particular value.

Another Example:

Output:

The New Operators

  • New Operator is used to creating pointers in golang by using new() function.
  • New accepts a type as an argument, allocates enough memory to hold a value of that type, and returns a pointer to that value.

For Example:

Output:

Run the code!

Nil Pointers

When we define but do not initialize a pointer variable, the value of the pointer is always considered as nil.

For Example:

Output:

Run the code!

In the above code, we don't initialize the ptr, so by default, it will give us nil as an output.

Dereferencing the Pointers

Dereferencing a pointer means retrieving the value contained within the address held by the pointer. If we know the memory address, we can dereference the pointer to that memory address to access the value stored there. Here's an example of a dereference operation using the star(*) operator.

For Example:

Output:

Run the code!

Pointers provide efficient access to variables. When passing complicated or huge data, it will take extra efficiency if you do not utilize pointers because you can only use copy methods when passing values.

However, there are a few considerations to make when using it:

  • Avoid using pointers for reference types such as channels.
  • Pointers are not necessary for basic data types like int and bool.
  • It is best to avoid nesting pointers, or connecting one pointer to another.
  • In Concurrently safe scenarios, pointers are useless.
  • Pointer arithmetic is prohibited in Go. As a result, we are unable to do operations like unary increment or decrement as we can in C/C++.
  • A pointer to an array might be what we want to utilise, but there is a better way to do it. And they are slices. Slices are generally more versatile than an array pointer. The code is clear and simplifies our lives greatly. So, whenever possible, use slices.

Conclusion

  • In this article we have seen the use of a pointer along with the new operator.
  • We learned how & and * we can use in golang program.
  • We also got familiar with the working of dereference pointers in going with an example and what happens when we do not initialise our pointer variable it will simply give us nil by default as an output.