Go Pointers
Links: 103 Golang Index
Why use pointers instead of returning values?
- When we pass values to functions a copy of them is passed.
- This isn't problematic when you are passing simple strings or ints which are small in size
- But if you are passing large data structures to functions a copy of it passed which is a waste of resources and might slow down your program
- In these cases it is better to pass pointers to these large data structures
Pointers¶
- The Computer Memory (RAM) can be thought of as a sequence of boxes or cells, placed one after another in a line.
- Each cell is labeled with a unique number (hexadecimal), which increments sequentially; this number is the address of the cell or its memory location.
- Each cell holds a single value. Everything the CPU does is fetching and storing values into memory cells.
- If a number starts with
0x
then it means it is a hexadecimal number and most likely an address.
What is a variable¶
- A variable is just a convenient, alphanumeric nickname or label for a memory location.
- When we write
var a int = 5
we create a label called a for a memory location where the value 5 of type int will be stored.
In a nutshell: memory is just a series of numbered cells, and variables are just nicknames for memory locations assigned by the compiler.
What is a pointer¶
- A pointer is a variable that stores the memory address of another variable.
The pointer points to memory address of a variable, just as a variable represents the memory address of a value.
-
A pointer value is the address of a variable or
nil
if it hasn't been initialised yet. -
In the below image, variable
b
has value156
and is stored at memory address0x1040a124
. The variablea
holds the address ofb
. Nowa
is said to point tob
ora
is a pointer tob
. -
Pointers have the power to mutate or change the data they are pointing to.
- With a pointer we can read or update the value of a variable directly without knowing the name of the variable.
Coding¶
- To get the memory address of a variable we use
&
- Pointer is represented as
*type
- Declaring a pointer
- Another way of creating a pointer is using the
new
keywordptr1 := new(int) // int pointer
- Dereferencing operator (
*
)- To find the value at the address pointer is pointing to
*
is also used for dereferencing apart from declaring the pointer*float64
(type declaration) is completely different from*p
- We can have a pointer to a pointer
- Comparing pointers
- Pointers are same if both of them are
nil
or they point to the same variable
- Pointers are same if both of them are
Passing pointers to functions¶
- Go allows to paste parameters to functions both by pointers and values
- If we want to change
int
,float
,bool
,string
,arrays
andstruct
values in a function rather than their copies then we will have to pass a pointer to the function. - Passing struct as a pointer
- But in case of slice and map when the function changes data actual data is changed.
It is not a good practice to pass arrays to functions instead we should pass slices.
In Go everything is passed by value
- In other languages when a pointer is passed to a function it is known as pass by reference and this is not correct.
- Each time we pass an argument to a function even if its a pointer that argument will be copied to a different memory location.
func f1(a *int) {
fmt.Println("Should be same in f1", a)
fmt.Println("Different in f1", &a)
}
func main() {
value := 1
ptrval := &value
fmt.Println("Should be same in main", ptrval)
fmt.Println("Different in main", &ptrval)
f1(ptrval)
}
// Should be same in main 0xc000018030
// Different in main 0xc00000e028
// Should be same in f1 0xc000018030
// Different in f1 0xc00000e038
Pass pointers to functions only when it is necessary. Pointers are expensive and put pressure on garbage collector.
Last updated: 2022-06-12