Go pointers
In Go parameters are passed by value; the value of a variable is passed to a function, and the function will use that value without modifying the original variable.
If we want the function to be able to access the variables outside, we need to pass it their pointers. Pointers are objects that provide the memory address of the variable.
A pointer holds the memory address of a value.
The &
operator generates a pointer to its operand.
E.g.:
1Double(&x)
we are passing the pointer to x
to the function Double
Pointers have their own type: a pointer to an integer has type *int
, a pointer to a floating point value has type *float64
and so on. You get the type of the pointer by preceding the type of the variable with *.
Pointers’ type is not just pointer: *int
is distinct from *float64
.
Inside the function, you can’t do operations with the pointer itself. Instead, you’ll have the de-reference it, that is, you’ll need to get the variable the pointer is pointing to.
The *
operator denotes the pointer’s underlying value.
E.g.:
1func Double(input *int) {
2 *input *= 2
3}
You dereference a pointer by preceding it with asterisk.
The default value of a pointer is nil
.
If you try to dereference nil
, you’ll get panic.
References
- John Arundel, For the Love of Go
Next -> pointer-methods-in-go
Next -> pointers-to-struct
#stack #indirecting #golang #reference #pass_by #value #programming #heap #pointer #for_the_love_of_go #dereferencing