What Are Pointers in Go and Why Use Them

Go pointers store memory addresses to modify variables directly and avoid expensive data copying.

Pointers in Go are variables that store the memory address of another variable, allowing you to modify the original value directly or pass large data structures efficiently without copying them. Use pointers when you need to mutate a value inside a function or avoid the performance cost of copying large structs.

package main

import "fmt"

func main() {
	// Create a variable
	val := 10
	// Create a pointer to that variable
	ptr := &val
	
	// Modify the value via the pointer
	*ptr = 20
	
	fmt.Println(val) // Output: 20
}