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:
- * – 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.
- & – 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:
For example, the code below declares a pointer variable “ptr” that holds the address of int types.
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:
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:
0xc000018098
We can also declare a variable and set the value later in the code. An example is as shown:
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:
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:
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:
To avoid this, you can use pointer literal method of declaration. For example, we can do:
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:
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:
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!