golang

Golang Pointers

A pointer in programming refers to the variable used to store memory address of another variable in the system. The memory address of variable is expressed in hexadecimal format starting with 0x.

Pointers are useful as they allow us to store the memory of an address where the data held by a variable is stored. Using pointers, we determine the value stored by the variable at that memory address.

In this beginner friendly article, we will discuss how to get started with pointers in the Go programming language.

Golang Declare Variable

We declare a pointer the same as we declare a normal variable. However, we use the dereferencing operator to show that the variable holds a pointer.

Before declaring a variable, there are two operators we need to be familiar with:

  1. * – The asterisk in pointer declaration is known as dereferencing operator. It is used to declare a pointer value and access the value stored at a specific memory address.
  2. & – In pointer, the address operator is used to access the memory address of a variable. It can also return the address of the variable.

The syntax below shows how to declare a pointer variable:

var pointer_name *data_type

For example, the code below declares a pointer variable “ptr” that holds the address of int types.

var ptr *int

Golang Initialize Pointer

We can initialize a pointer by allocating the memory address of a variable using the address operator, shown above.

An example is as shown:

package main
import "fmt"
func main() {
    // declare variable
    x := 100
    // declare and initialize pointer
    var ptr *int = &x

    // print address
    fmt.Println(ptr)
}

In the example above, we start by declaring a variable holding an integer value. We then create a pointer holding the memory address of the variable.

Once we print the address, we should get an example output as shown:

$ go run pointer.go
0xc000018098

We can also declare a variable and set the value later in the code. An example is as shown:

package main
import "fmt"
func main() {
    // declare variable
    x := 100
    // declare
    var ptr *int
    ptr = &x

    // print address
    fmt.Println(ptr)
}

Here, we declare the pointer and later set its value using the address operator.

Golang Nil Pointer

If you declare a pointer and not assign a value, the pointer holds a nil value. For example:

package main
import "fmt"
func main() {
    var ptr *int
    fmt.Println(ptr)
}

The code above returns nil.

Pointer Type

In our example, we declared the type of the pointer. This means the pointer can only hold memory addressed of the specified data type.

If you try to store the address of another type, the compiler returns an error as shown in the example below:

package main
import "fmt"
func main() {
    // declare variable
    str := "hello"
    var ptr *int
    ptr = &str
    // print address
    fmt.Println(ptr)
}

In the example above, we declare a variable with type int. However, we assign an address of a string type.

The compiler returns an error as:

.\pointer.go:11:6: cannot use &str (type *string) as type *int in assignment

To avoid this, you can use pointer literal method of declaration. For example, we can do:

package main

import "fmt"
func main() {
    str := "hello"
    ptr := &str
    fmt.Println(ptr)
}

The example above allows the compiler to determine the type of address to hold in the specified pointer.

Golang Pointer Dereference

To access the value stored at a specific address, we can use the dereferencing operator. An example code is as shown:

package main
import "fmt"
func main() {
    x := 100
    ptr := &x
    fmt.Println("x: ", x)
    fmt.Println("Address: ", ptr)
    fmt.Println("Value at address: ", *ptr)
}

The above code uses the dereferencing operator to get the value stored at the address held by the pointer “ptr”

The resulting output is as:

x:  100
Address:  0xc000018098
Value at address:  100

Conclusion

This guide walks you through the basics of working with pointers in Go. Using pointers, you can access and store the memory address of a variable. You can also use the pointer to access the variable stored at a specific memory address.

Thanks for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list