0
0
GoComparisonBeginner · 4 min read

Pointer vs Value in Go: Key Differences and When to Use Each

In Go, a value holds the actual data, while a pointer holds the memory address of that data. Using pointers allows you to modify the original data and save memory by avoiding copies, whereas values work with independent copies.
⚖️

Quick Comparison

This table summarizes the main differences between pointers and values in Go.

AspectValuePointer
Data storedActual dataMemory address of data
Memory usageCopies data, uses more memoryStores address, uses less memory
Modification effectChanges affect only the copyChanges affect original data
Default zero valueZero value of the type (e.g., 0, "", false)nil (no address)
SyntaxDirect variable usageUse * to dereference, & to get address
Use caseWhen data copy is cheap or isolation neededWhen sharing or modifying original data
⚖️

Key Differences

Values in Go hold the actual data. When you assign or pass a value, Go copies the entire data. This means changes to the copy do not affect the original variable. This is simple and safe but can be inefficient for large data.

Pointers hold the memory address where the data lives. Instead of copying data, pointers let you work with the original data directly. You can change the original data through a pointer, which is useful for efficiency and when you want to share data between functions.

Using pointers requires understanding how to get the address with & and access the data with *. Values are easier to use but less flexible for certain tasks like modifying shared data or saving memory.

⚖️

Code Comparison

This example shows how to update a number using a value copy. The original number does not change.

go
package main

import "fmt"

func updateValue(num int) {
    num = num + 10
    fmt.Println("Inside updateValue:", num)
}

func main() {
    number := 5
    updateValue(number)
    fmt.Println("After updateValue call:", number)
}
Output
Inside updateValue: 15 After updateValue call: 5
↔️

Pointer Equivalent

This example shows how to update a number using a pointer. The original number changes because the function works with its address.

go
package main

import "fmt"

func updatePointer(num *int) {
    *num = *num + 10
    fmt.Println("Inside updatePointer:", *num)
}

func main() {
    number := 5
    updatePointer(&number)
    fmt.Println("After updatePointer call:", number)
}
Output
Inside updatePointer: 15 After updatePointer call: 15
🎯

When to Use Which

Choose values when you want simple code and your data is small or you want to avoid side effects. Values are great for independent copies and safer concurrency.

Choose pointers when you need to modify the original data, avoid copying large structs or arrays, or share data efficiently between functions. Pointers give you control and better performance for big data.

Key Takeaways

Values hold actual data and copying them creates independent copies.
Pointers hold addresses and allow modifying the original data.
Use values for simplicity and safety with small data.
Use pointers for efficiency and when you need to change shared data.
Remember to use & to get addresses and * to access pointer data.