go / beginner
Snippet
Pointers: Direct Memory Access
Pointers store memory addresses rather than values. The & operator gets the address of a variable, while * dereferences a pointer to access the underlying value. Pointers are especially useful for modifying variables in functions and managing large data structures efficiently.
snippet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package mainimport "fmt"func main() {count := 10ptr := &countfmt.Println("Value:", count)fmt.Println("Address:", ptr)fmt.Println("Dereferenced:", *ptr)*ptr = 20fmt.Println("New value:", count)}
Breakdown
1
ptr := &count
& gets memory address of count variable
2
*ptr
* dereferences pointer to get stored value
3
*ptr = 20
Modifying dereferenced value changes original variable