This is my note of using Go pointer; how it made, how to utilize it, and what kind of common case we encounter a pointer. First note is how pointer created in Go. ✓
var mem_add *dtype
Star notation (*
) used to mark a variable that it is a pointer; it only contain certain memory address. Pointer need data type (dtype
).
Assigning different data type to a memory address with different data type will cause an error.
address0 = &number
Ampersand notation (&
) used to extract an address from a variable. In example above, we assign address0
to have memory address of variable number
.
*address0 = 88
We use (*
) to assign a value to a certain address.
Basic rule is
. accessing memory address from a value is using ampersand line 9
. accessing value from a memory address is using star line 18
same rule is also applied when declaring