Understanding pointers in Golang
In this post, we will learn pointers in Golang and how they are used to store memory addresses of variables.
What is a pointer?
A pointer is a variable that stores the memory address of another variable.
In Golang, pointers are used to pass references to values instead of the values themselves. This allows us to modify the original value from within a function or method.
Declaring a pointer
To declare a pointer in Golang, we use the *
symbol followed by the type of the variable that the pointer will point to. For example, to declare a pointer to an integer variable, we can write:
var ptr *int
This declares a pointer variable ptr
that can store the memory address of an integer variable.
Initializing a pointer
To declare a pointer, use the *
symbol before the type.
To get the address of a variable, use the &
operator.
var x int = 42
var ptr *int = &x // ptr is a pointer to x
Here:
x
is an integer variable with the value 42.ptr
is a pointer tox
, storing the memory address ofx
.
This initializes the pointer ptr
with the memory address of the integer variable num
.
Dereferencing a pointer
To access or modify the value stored at the memory address a pointer points to, use the *
operator (dereferencing).
fmt.Println(*p) // Output: 42 (value of x)
*p = 100 // Modify the value of x through the pointer
fmt.Println(x) // Output: 100
This will print the value 42
, which is the value stored in the integer variable num
.
Modifying the value of a variable using a pointer
Pointers in Golang can be used to modify the value of a variable indirectly. To modify the value of a variable using a pointer, we dereference the pointer and assign a new value to it. For example, to modify the value of the integer variable num
using the pointer ptr
, we can write:
*ptr = 100
fmt.Println(num)
This will print the value 100
, which is the new value of the integer variable num
.
Pointers with Functions
Pointers are often used in functions to pass variables by reference, allowing the function to modify the original variable.
func increment(ptr *int) {
*ptr++ // Increment the value at the address p
}
func main() {
var num int = 42
increment(&num)
fmt.Println(num) // Output: 43
}
Pointers with Arrays
Pointers are also commonly used with arrays in Golang to access and modify array elements efficiently. For example, consider the following code snippet that uses a pointer to iterate over an array and modify its elements:
func main() {
var arr [5]int = [5]int{1, 2, 3, 4, 5}
var ptr *int = &arr[0]
for i := 0; i < len(arr); i++ {
fmt.Println(*ptr)
ptr++
}
}
In this example, we declare an integer array arr
with 5 elements and initialize it with some values. We then declare a pointer ptr
to an integer variable and initialize it with the memory address of the first element of the array. We use the pointer ptr
to iterate over the array and print its elements.
Common Use-cases
Pointers are commonly used in Golang for the following purposes:
- To pass references to values instead of the values themselves to functions.
- To modify the original value of a variable from within a function.
- To access and modify array elements efficiently.
- To optimize memory usage by avoiding unnecessary copying of values.
Examples
Memory Address of a Variable
package main
import "fmt"
func main() {
var num int = 42
var ptr *int = &num
fmt.Println("Value of num:", num) // 42
fmt.Println("Memory address of num:", &num) // 0x1400009c018
fmt.Println("Value of ptr:", ptr) // 0x1400009c018
fmt.Println("Value pointed to by ptr:", *ptr) // 42
}