0
0
Goprogramming~10 mins

Why pointers are needed in Go - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why pointers are needed
Start with variable x
Pass x to function
Function receives copy of x
Modify copy inside function
Return from function
Original x unchanged
Use pointer to x
Function receives address of x
Modify value at address
Return from function
Original x changed
End
This flow shows how passing a variable copies it, so changes don't affect the original, but passing a pointer lets the function change the original variable.
Execution Sample
Go
package main
import "fmt"
func changeVal(val int) { val = 10 }
func changePtr(ptr *int) { *ptr = 10 }
func main() {
  x := 5
  changeVal(x)
  fmt.Println(x)
  changePtr(&x)
  fmt.Println(x)
}
This code shows how changing a value copy does not affect the original, but changing via pointer does.
Execution Table
StepVariableValueActionEffect
1x5Initialize x with 5x = 5
2val5changeVal called with val = x (copy)val = 5 (copy)
3val10Inside changeVal, val set to 10Only val changes, x unchanged
4x5Back in main, x still 5No change to x
5ptraddress of xchangePtr called with pointer to xptr points to x
6*ptr10Inside changePtr, *ptr set to 10x value changed via pointer
7x10Back in main, x is now 10x changed by pointer
8--Program endsFinal x = 10
💡 Program ends after demonstrating value copy does not change x, but pointer does.
Variable Tracker
VariableStartAfter changeValAfter changePtrFinal
x551010
val-10--
ptr--address of x-
Key Moments - 3 Insights
Why does x not change after calling changeVal(x)?
Because changeVal receives a copy of x (val), so modifying val does not affect the original x. See execution_table step 3 and 4.
How does changePtr modify the original x?
changePtr receives a pointer to x (address), so modifying *ptr changes the value at that address, which is x. See execution_table step 6 and 7.
What is the difference between passing a value and passing a pointer?
Passing a value copies the data, so changes inside the function don't affect the original. Passing a pointer passes the address, so changes affect the original variable. See the whole execution flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of x?
A10
B5
C0
DUndefined
💡 Hint
Check the 'Value' column for x at step 4 in execution_table.
At which step does the original x change value?
AStep 7
BStep 4
CStep 3
DStep 2
💡 Hint
Look for when x's value changes from 5 to 10 in variable_tracker and execution_table.
If changeVal modified a pointer instead of a value, what would happen?
Ax would remain unchanged
BProgram would not compile
Cx would change value
Dx would become zero
💡 Hint
Refer to how changePtr modifies x via pointer in execution_table steps 5-7.
Concept Snapshot
Pointers hold memory addresses.
Passing a value copies data; changes don't affect original.
Passing a pointer lets functions modify original data.
Use * to access value at pointer.
Use & to get address of variable.
Pointers enable efficient and direct data changes.
Full Transcript
This visual trace shows why pointers are needed in Go. When you pass a variable to a function, Go copies its value. So if the function changes the copy, the original stays the same. This is shown by the changeVal function which sets its parameter to 10, but the original variable x remains 5. To change the original variable, you pass its address using a pointer. The changePtr function takes a pointer to x and changes the value at that address. This changes x itself. The execution table shows each step, tracking variable values and actions. The variable tracker summarizes how x stays 5 after changeVal but becomes 10 after changePtr. Key moments clarify common confusions about copying vs pointer passing. The quiz tests understanding of when and how x changes. The snapshot summarizes pointers as tools to directly modify original data by passing addresses instead of copies.