Pointers store the memory address of a variable instead of its value. They allow you to directly manipulate the data stored in memory — a powerful feature in Go for performance and flexibility.
var x int = 42 var p *int = &x // p stores address of x fmt.Println(x) // 42 fmt.Println(p) // memory address of x fmt.Println(*p) // 42 (value at address p)
Explanation: By passing a pointer to x
, the increment
function directly modifies x
’s value in memory. This is efficient and useful for changing variables outside the function scope.
Symbol | Meaning | Example |
---|---|---|
& | Address of operator (gets memory address) | p = &x |
* | Dereference operator (gets value at memory address) | val = *p |
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!