Pointers
Sample code link: (https://repl.it/@jjoco/go-pointers)
Pointers essentially points to a place in memory that stores a variable of type T (ie memory address). This is similar to pointers used in C-like languages.
Regular vs. Short Assignment
One can define pointers via regular or short assigning, like any other variable. Dereferencing them is identical to that of other languages, eg if iPtr is a pointer, *iPtr dereferences it.
Regular
1 2 3 4 5 6 7 |
|
1 2 3 4 5 |
|
Pass by Value vs. Pass By Pointer
If you intend to change the input variable to a function and have the change be visible from the caller, use pointers.
Pass by Value
1 2 3 4 |
|
1 2 3 4 |
|
1 2 3 4 |
|
1 2 3 4 |
|
However, in the Pass By Pointer case, the address of l is passed into doubleByPointer, which doubles the integer that is stored in that memory address. As a result, the main function observes l double.
Pointers to Pointers
A developer can set a variable to be pointers to other pointers. For example, if I want to make a pointer to a pointer (double pointer), one can declare var doublePointer **int
. It follows that one can declare a triple pointer (pointer to a double pointer) like var triplePointer ***int
, and so on and so forth.