Learning basics concepts about Pointers in Golang
Published: 2023-04-13 13:14:03

Hello Dev, This is my very first article and I would like to start explaining a little bit about a concept that I took time to understand, We are going to talk about pointers in go, but the concept is not very different from other languages like **C** and **C++**. Golang has something that is called **Garbage Collector** this tool has the responsablity to do memory management automatically, in the primitive languages(**C** & **C++**) you as developer need to allocate and dealocate it explicitly.

Now let's start defining what is a variable, Yes, It's important to refresh the concept of varible to understand pointers, Well, As you probably know variable is the reference of locations in memory that we store values, And as every location it has an address, when you declare a variable in go like:

```
package main

func main(){
   var name string
   name = "Bento Julio"

}
```
A simply representation of what is happening when you run the code above is in the floowing image:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ad1w6129pse6cefyqod6.png)



As you can see every variable has a value and an address, This is where we begin with pointers, Pointers are variable that store the value of address of another variables, In Golang we define a pointer variable using the _*_ we call it _dereferecing operator_ and we also use _&_ to get the address of a variable and this operator is called _address-of operator_ , Now that we know what are pointers, Let's see a simple code and it's representation in memory:

```
package main

import "fmt"
func main(){
   var namePointer *string

   name := "Bento Julio"

   namePointer = &name

   *namePointer = "Kenny Mario"
    fmt.Println("Value of Name:", name)
    fmt.Println("Value of Name Pointer:", namePointer)
}
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7vxi2c7ut7t5s3qmzim.png)



As you can see the value of variable **name** is different from the value of **namePointer** variable, It's cause the first variable is a string and the second one is a pointer of string and we can get the value of **name** if we put _dereferecing operator_ in front of **namePointer**, It's cause this variable is storing the address of the variable **name** as we can see int the output of the program above, Let's see the representation in memory:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/45rpuv4b3q7wel69ln46.png)



Now that you know what are pointers and how to use them in Golang we will see another use case of pointers, it's when we are using function, In Golang when you pass the arguments to a function you're not passing the variable, but you're just giving to the function a copy of the value of this variable, That's why every change that you do in the parameter inside the function it will not change the original value of the variable, Let's give an example:

```
package main

import "fmt"

func sumPlusTwoValue(numberParam int) {
   numberParam += 2
}
func main(){
   numberArg := 2
   fmt.Println("Value of Number:", numberArg) 
   sumPlusTwoValue(numberArg)
   fmt.Println("Value of Number in After add 2 in Function sumPlusTwoValue:", numberArg)

}
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yeo3xykpu92rf8nq4lim.png)


As you can see the value of variable **numberArg** did not change and like we saw before we're not passing the variable **numberArg ** to function _sumPlusTwoValue_ we just passed a copy of his value.

## What if we want to change the value of variable **numberArg**?

Well, to do that we need to make a simple refactoration to our code, first we will change the signature of the function _sumPlusTwoValue_, instead of the parameter type be a simple integer we will define the type of param to pointer of integer, and in the function main we will pass the argument instead of the value we will pass the address of variable **numberArg**, Let's make the refactoration of our code:

```
package main

import "fmt"

func sumPlusTwoValue(numberParam *int) {
   *numberParam += 2
}
func main(){
   numberArg := 2
   fmt.Println("Value of Number:", numberArg) 
   sumPlusTwoValue(&numberArg)
   fmt.Println("Value of Number in After add 2 in Function sumPlusTwoValue:", numberArg)

}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ntvr6at2xk31jd8nd93.png)



Well as you see in the output we changed the value of variable **numberArg** inside the function _sumPlusTwoValue_, It's cause this time we did'nt pass the copy of the value of variable **numberArg**, but we passed to argument of function _sumPlusTwoValue_ the real address of this variable.

## CONCLUSION

Pointer is a simple concept but it's a little bit hard to understand in the beginning, You should read this article more than once, more you read better will be your comprehension about pointers. We saw ho important they are to manage the values of variables outside of where it was created.

Thanks to my friend [Márcio](https://ao.linkedin.com/in/m%C3%A1rcio-comba-283a581a0), Helped me to write this article.

Thanks for you too reader and I wish you a good study.