Understanding Pointers in Go.

Understanding Pointers in Go.

Introduction

When I started learning the Go programming language, one of the topics that confused me for a while was Pointers.
I was perplexed as to why pointers are required, as well as their primary use. I eventually grew used to using pointers when working on a project, but I couldn't explain its functionality to others or even myself.
According to a webpage on A Tour of Go - A pointer holds the memory address of a value, which means that Pointers are simply unique variables used to store another variable's memory address.

I. Differences between Variables and Pointers in Go.

A variable is used in computer programming to store data that can be altered at any time, whereas a pointer just saves and points to another memory address. The keyword var can be used to declare a single variable or a group of variables. Consider the following examples:

  • var letter string = "A" - This will provide an output of A, as it is a variable of type string.
  • var age int = 4 - This will provide an output of 4, as it was also typed as an integer.
  • var age int - This will provide the output of 0 because we have not yet initialised any value to this variable, and if a defined value is not provided, Go automatically initialises it to zero.
  • letter := "B" - A quick way to declare variables in Go.

II. How Pointers can be declared in Go.

The * and & operators are the two operators that Go pointers work with. The * operator declares a Go pointer variable and it is used to return the value of the variable stored in the memory address, whereas the & operator returns a variable's memory address. The provided name of the pointer and the data type is required to declare a pointer.
To point to its memory address, a Go pointer can be initialized to a variable. For example, if var number int = 24 is a declared variable, we can initialize a pointer with var count *int = &number.

III. How pointers truly work.

Let's address the basic topic of why we need pointers and how they work now that we've gotten a quick overview of pointers and variables in Go. One important element to remember about pointers is that they maximize efficiency.

Let's create a simple Go function below:

package main

import "fmt"

func newWord(word string) {
    word = "sad"
}

func main() {
    // var word string
    // word = "happy"
    word := "happy"
    newWord(word)
    fmt.Println(word)
}

A function called newWord is declared in the code above, and it takes in an argument called word, which is a string data type. The argument is initialized with the value sad within the function.
Now, using the Go short variable declaration, a variable called word is declared and initialized to the value happy in the main function. The function's commented code also demonstrates another technique to declare variables.
Before printing the output, the newWord function is called and the variable word is provided as a parameter/argument.

Because the variable word embedded within the main function does not have access to the same variable in the newWord function and views the value of the word variable within the main function as reassignment, the output of this operation will be happy.

And this is where pointers come in handy since they allow you to access another variable's memory address rather than having to reinitialize the same or another value to the same variable.

package main

import "fmt"

func newWord(word *string) {
    *word = "sad"
}
func main() {
    word := "happy"
    newWord(&word)
    fmt.Println(word)
}

The second piece of code above is identical to the first one covered previously, except this time the functions include pointer operators. The * operator is added to the front of the data type of the argument in the newWord function to declare a Go pointer variable. It stores the memory address of the variable.
The newWord function is called in the main function, and the variable word is passed in as an argument, but with the & operator in front of it. This means that the function should be able to access or return the memory address of the variable. The result above will be sad and not happy.

Finally, I'm delighted I was able to share how I learned to correctly grasp Go Pointers. And thank you so much for taking the time to read!!!

References:

  1. Go Resources. (n.d.). Pointers. golang-book.com/books/intro/8