The type *T
is a pointer to a T
value. It's zero value is nil
var p *int
creates a pointer to an int
&
generates a pointer to its operand - its used to retrieve the address, and the address is what is stored
*
denotes the pointer's underlying value - this retrieves the value at an address (dereferencing)
a := 1 // define int b := 2 // define int ap = &a // set ap to address of a (&a) // ap address : 0x2101f1018 (ap, or the address of a) // ap value : 1 (*ap) // ap own address: 0x5559a0 (&ap) *ap = 3 // change the value at address &a to 3 // ap address: 0x2101f1018 // ap value : 3 (*ap) a = 4 // change the value of a to 4 // ap address: 0x2101f1018 // ap value : 4 (*ap) ap = &b // set ap to the address of b (&b) // ap address: 0x2101f1020 // ap value : 2 (*ap)
https://play.golang.org/p/wNAr8vqmU0z
In this case, ap
is a *int
, and thus cannot be directly assigned to an int (e.g. ap = 4
)
TODO - pointer with functions in Go https://appliedgo.com/courses/128278/lectures/2652320