In this post we will explore about pointers in Go:
- What are Pointers?
- Why does feature exist?
- How can you work with pointers?
We will work with addresses instead of values.
What exactly are "Pointers"?
Pointers are variables that store values addresses instead of values
It may sound confusing so let's look how your code affects your computer memory
Let's say you have create and initialize variable called age set the value of 32 that value 32 stored in a computer's memory that value in memory get automatically address and that address is required by the computer to be able to retrieve that value and work with it.
But in this case here this value 32 which in our code which stored in variable age is actually also stored in computer memory and every value use in Go is atleast for short period of time stored some where in your computer's memory.
Now a pointer then is variable where you don't store a value but where you instead use & -> 'ampersand' to get an store the address of value instead of the value itself so &age pointer would contain the address as value.
Why "Pointers"?
When working with pointers you can avoid unnecessary value copies and other benefit of pointers is you can use pointers to directly mutate values
but what exactly do i means with this above advantages? so let's start with these advantage
Avoid unnecessary value copies
- By default,Go creates a copy when passing values to functions
- For very large and complex values, this may take up too much memory space unnecessarily
- With pointers, only one value is stored in memory(and the address is passed around)
Directly mutate values
- Pass a pointer(address) instead of a value to a function
- The function can then directly edit the underlying value - no return value is required
- Can lead to a less code (but also to less understandable code or unexpected behaviours)
Pointers in Go (also known as Golang) are variables that store the memory address of another variable. Here's a simplified explanation:
Declaring Pointers
In Go, pointers are declared using the asterisk symbol (*) before the pointer name. For example:
var ptr *int
This declares a pointer named ptr that points to an int value.
Getting the Address of a Variable
To get the memory address of a variable, use the address-of operator (&). For example:
x := 10
ptr = &x
This sets the pointer ptr to the memory address of the variable x.
Dereferencing Pointers
To access the value stored at the memory address held by a pointer, use the dereference operator (*). For example:
y := *ptr
This sets the variable y to the value stored at the memory address held by ptr, which is 10.
Conclusion
Pointers in Go are used to:
- Pass variables by reference to functions
- Return multiple values from functions
- Create data structures like linked lists and trees
- Improve performance by avoiding copying large data structures
However, Go's pointers are:
- Safe: Go's pointers are bounds-checked, preventing common errors like null pointer dereferences or out-of-bounds access.
- Limited: Go's pointers cannot be arithmetic or cast to other types, making them less error-prone.
Top comments (0)