Golang Tutorial — How to use Pointers
The languages such as Java, C/C++, and Go have pointer concept. It is a concept that allows us to manipulate variables in the memory address form. Pointer is not a big topic in Golang but it is used almost everywhere in your code, if you’re working on high performance and high standard Go project. If you want to know what it is, so let’s find out now!
What is pointer?
Let’s say there is a variable, and it’s used in multiple functions at the same time. You want it to be updated automatically whenever any function changes its value to let the others can get the new value. Pointer is the concept that can solve this situation.
As you can see in the example above, the value of x isn’t changed after calling the function, different from the value of y that’s changed after calling the function. It is because Go is passing variable to a function by value not reference. Means that x in main function and a in update function, they are not the same one, and for y and b as well. But the difference is before passing y to the update function, it is casted to pointer and sent to the function. So, the function will get the memory address of y and uses it to change the value of y directly.
Declaration
Similar to other variable type declarations, pointer uses the same format but you need to put star (*) in front of the variable type you need to declare the pointer of it. As you can see, nil is the zero value of pointer no matter what kind of variable type the pointer points to.
For some variable type, such as slice and map, the identifier of these variable types are pointer by default. You might see that when you print their value, it won’t display nil but they are actually nil if you compare the values with nil.
So you can update the value of these kinds of variable inside other functions just like the pointer of other variable types. Anyway, you can use pointer with these kinds of variable, but it’s very rare case that you need to use pointer with them.
Your customized variable type and struct also can be used with pointer, and it doesn’t change the way to use your struct that much. What you need to do is just to assign the address to your struct pointer before using it which I will explain to you in next topic!
Get Address
In order to use get an address from a variable, you need to put ampersand (&) in front of the variable you need to get its address.
You also can declare a pointer by assigning address from other variable but you can’t get address from basic type data like string, number, and boolean.
Anyway, for some composite type, such as struct, you can get address without declaring other variable to get the address from.
Get Value
Similar to address getting, you can get the value from pointer by putting star (*) in front of the pointer
Example
For someone who’s new with the languages like Go, and C/C++, you might not see the way to make it useful in any real project. So, I will show you an example of what I think you must use in the future, if you’re going to use Golang with your project.
This is how to encode and decode JSON data. As you can see in the parameters of json.Unmarshal, there is no need to return the result, but just change the second parameter’s value by using pointer.
Anyway, there is still be other reasons why so many libraries including this one need to use pointer instead of returning the value, and I will explain to you in the next article which is about Method and Interface. Please stay tuned, thanks 😎