0
0
Goprogramming~10 mins

Pointer declaration in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pointer declaration
Declare variable
Declare pointer variable
Assign address of variable to pointer
Use pointer to access value
Modify value via pointer
End
This flow shows how to declare a variable, then declare a pointer to it, assign the address, and use the pointer to read or change the value.
Execution Sample
Go
package main
import "fmt"
func main() {
	var x int = 10
	var p *int
	p = &x
	fmt.Println(*p)
	*p = 20
	fmt.Println(x)
}
Declare an int variable x, declare a pointer p to int, assign p the address of x, print value via pointer, change value via pointer, print x.
Execution Table
StepActionVariableValuePointer ValueOutput
1Declare xx10undefined
2Declare ppundefinednil
3Assign p = &xpundefinedaddress_of_x
4Print *px10address_of_x10
5Set *p = 20x20address_of_x
6Print xx20address_of_x20
💡 Program ends after printing updated x value 20.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5Final
xundefined1010102020
pundefinedundefinedniladdress_of_xaddress_of_xaddress_of_x
Key Moments - 2 Insights
Why does *p print 10 at step 4 instead of the address?
Because *p means 'value at the address p points to', so it prints the value of x, which is 10 (see execution_table step 4).
Why does changing *p at step 5 change x's value?
Because p points to x's address, so *p = 20 updates x directly (see execution_table step 5 and variable_tracker for x).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of p?
Anil
Baddress_of_x
C10
Dundefined
💡 Hint
Check the 'Pointer Value' column at step 3 in execution_table.
At which step does the value of x change from 10 to 20?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look at the 'Value' column for x in execution_table between steps 4 and 5.
If we print p instead of *p at step 4, what would be the output?
A20
B10
Caddress_of_x
Dnil
💡 Hint
Pointer p holds the address of x, see 'Pointer Value' column in execution_table step 4.
Concept Snapshot
Pointer declaration in Go:
var x int = 10       // declare variable
var p *int           // declare pointer to int
p = &x               // assign address of x to p
*p accesses value at p's address
Changing *p changes x's value
Full Transcript
This example shows how to declare a variable x and a pointer p to int in Go. We assign p the address of x using &x. Printing *p shows the value of x. Changing *p changes x's value because p points to x. The execution table traces each step, showing variable and pointer values and outputs. Key moments clarify why *p prints the value, not the address, and how changing *p updates x. The quiz tests understanding of pointer values and when x changes.