Pointers in Go allow you to reference a variable's memory address so you can directly access and modify the value stored at that location.
&: Get the memory address of a variable.
*: Access or modify the value stored at a pointer's address.
func main() { // Declaring a variable: a := 10 fmt.Println(a) // 10 fmt.Printf("%T\n", a) // int // Declaring a pointer and assigning it the address of 'a': ptr := &a fmt.Println(ptr) // 0xc0000a4010 (memory address of 'a') fmt.Printf("%T\n", ptr) // *int fmt.Println(*ptr) // 10 (value at the memory address) // Changing the value at the memory address: *ptr = 20 fmt.Println(a) // 20}
By using pointers as function parameters, you pass the memory address of a variable rather than a copy of its value. This changes Go's default pass-by-value behavior and allows the function to modify the original data directly.
Since arrays are fully independent pass-by-value types in Go (like booleans, numerics, and structs), passing them normally creates a full copy. Using pointers avoids that copying.