Notessh2a

Pointers

A pointer is a variable that stores the location of a value in memory. It lets you work with the original value directly instead of making a copy.

var p *int

Zero value: nil.

  • &p: Address of p.
  • *p: Value pointed to by p (dereferencing).
func main() {
	a := 10

	fmt.Println(a)        // 10
	fmt.Printf("%T\n", a) // int

	// Declare a pointer and assign it the address of `a`:
	ptr := &a

	fmt.Println(ptr)        // 0xed92788e008
	fmt.Printf("%T\n", ptr) // *int
	fmt.Println(*ptr)       // 10

	// Change the value through the pointer:
	*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 here and allows the function to modify the original data directly.

func main() {
	num := 10
	fmt.Println(num) // 10

	resetVal(num)
	fmt.Println(num) // 10

	resetPtr(&num)
	fmt.Println(num) // 0
}

func resetVal(val int) {
	val = 0
}

func resetPtr(val *int) {
	*val = 0
}

Pointers as function parameters also allow you to work with large data structures more efficiently.

No unnecessary copying = Better performance.

  • Without pointer:

    func main() {
    	arr := [5]int{1, 2, 3, 4, 5}
    
    	fmt.Printf("mem addr (arr): %p, arr: %v\n", &arr, arr) // mem addr (arr): 0xc00011e000, arr: [1 2 3 4 5]
    	myFunc(arr) // mem addr (items): 0xc00011e060, items: [1 2 3 4 5]
    }
    
    func myFunc(items [5]int) [5]int {
    	fmt.Printf("mem addr (items): %p, items: %v\n", &items, items)
    	return items
    }
  • With pointer:

    func main() {
    	arr := [5]int{1, 2, 3, 4, 5}
    
    	fmt.Printf("mem addr (arr): %p, arr: %v\n", &arr, arr) // mem addr (arr): 0xc00011e000, arr: [1 2 3 4 5]
    	myFunc(&arr) // mem addr (items): 0xc00011e000, items: [1 2 3 4 5]
    }
    
    func myFunc(items *[5]int) [5]int {
    	fmt.Printf("mem addr (items): %p, items: %v\n", items, *items)
    	return *items
    }

    Since arrays are pass-by-value types in Go (like booleans, numerics, and structs), passing them normally creates a full copy. Using pointers avoids that copying.