DEV Community

Cover image for Learning basics concepts about Pointers in Golang
Bento Julio
Bento Julio

Posted on

Learning basics concepts about Pointers in Golang

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"

}
Enter fullscreen mode Exit fullscreen mode

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

Image description

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)
}
Enter fullscreen mode Exit fullscreen mode

Image description

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

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)

}
Enter fullscreen mode Exit fullscreen mode

Image description

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)

}
Enter fullscreen mode Exit fullscreen mode

Image description

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, Helped me to write this article.

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

Top comments (0)