How to Use Pointers in Golang

In this class, we discuss how to use pointers in Golang. By the end of the class, you’ll have learnt how to define and use pointers in Golang.

Let’s start the discussion by first defining pointers.

A pointer is an object whose value is used to refer to another value that’s stored elsewhere in a computer’s memory using its address. When you define a pointer in Golang, it will reference to a particular location in your memory and get the value that’s stored at that memory location through a process known as “dereferencing the pointer.”

Let’s take a look at the Golang code below that doesn’t use pointers :

package main
import "fmt"
func total (number int32) {
number = number + 1000
func main() {
number := 2000
total (number)
fmt.Println ("The total is:", number)

What do you think will be the output of these Golang code? Is it 2000 or 3000?
Well, the output of this program will be:

The total is: 2000

I bet that you’ll have expected the code to print 3000. Why is this the case? By default, all arguments are copied by value when passing in an argument. Therefore, the copy of the number variable is used to by the total function. That’s why the program prints 2000 instead of 3000.

How can you fix this problem? Good question.

You can either use the call by reference or use pointers. Here’s is how you can use the call by reference:

package main
import "fmt"
func total (number *int32) {
*number = * number + 1000
func main() {
number: = 2000
total (&number)
fmt.Println ("number:", val)

Notice the use of “*” and the ampersand character”&” in the above code. The total function has been made to accept an integer—which is a number—that references some memory. This pointer points to a memory location and allows you to change its value. When you call the total function in the main function, all you need is to send the reference—which is an address of the number variable.
The use of pointers in Golang helps to pass references to values and records within the Golang program. Therefore, the output of such a program becomes:

The total is: 3000

What you should know about Golang’s pointers
Here’s what you need to know when using Golang’s pointers:

    • •The Golang’s pointer is used to store the memory address of a given variable. The type *P means a pointer to the P value. By default, it will be set to zero value if it’s numeric or “nil” if it’s a string.


    • •We declare pointers using the syntax below:
var name_of_the_pointer *data type

In the above declaration, name_of_the_pointer is the name of the pointer. This is followed by an asterisk (*) and the data type—which can be numeric or non-numeric.

•Pointers can be initialized using the syntax below:
name_of_the_pointer:=some value. Here’s an example:

p=: 3830

•The “&” operator creates a pointer and pins it to its operand. For instance, the code “p= &M”denotes the pointer’s underlying value. This is referred to as dereferencing.

The & M syntax allocates the memory address of P, which is a pointer to M.

•Pointers can also be created using the “new” function. The new function takes a data type as an argument and allocates it enough memory so that it fits a value of that data type—and returns a pointer to the same data type. Here’s an example:

func example3 (p *int32) {
*p = 1000
func main () {
p: = new(int)
  example3 (p)
  fmt.Println (*p) 

Now you know how to use pointers in Golang. Use the knowledge you’ve learnt in this blog post to create programs that use pointers.


(Visited 1,273 times, 1 visits today)