📖 Scenario: Imagine you have a list of boxes, each with a number inside. You want to change the number inside a box without making a copy of the box. This is like using pointers in Go.
🎯 Goal: You will create a simple Go program that shows how pointers let you change the original value of a variable by using its address.
📋 What You'll Learn
Create an integer variable called number with the value 10
Create a pointer variable called ptr that points to number
Change the value of number through the pointer ptr to 20
Print the value of number to show it has changed
💡 Why This Matters
🌍 Real World
Pointers are used in real programs to efficiently update data without copying it, like changing settings or updating large data structures.
💼 Career
Understanding pointers is important for Go developers to write efficient and clear code, especially when working with functions, data structures, and system programming.
Progress0 / 4 steps
1
Create an integer variable
Create an integer variable called number and set it to 10.
Go
Hint
Use := to create and set the variable in one line.
2
Create a pointer to the variable
Create a pointer variable called ptr that stores the address of number using the & operator.
Go
Hint
Use &number to get the address of the variable number.
3
Change the value using the pointer
Use the pointer ptr to change the value of number to 20. Use the * operator to access the value at the pointer.
Go
Hint
Use *ptr = 20 to set the value at the address stored in ptr.
4
Print the changed value
Print the value of number using fmt.Println(number) to show it has changed to 20. Remember to import fmt.
Go
Hint
Use fmt.Println(number) to print the value.
Practice
(1/5)
1. Why do we need pointers in Go programming?
easy
A. To make the program run slower
B. To allow functions to modify the original variable's value
C. To create new variables automatically
D. To avoid using variables altogether
Solution
Step 1: Understand what pointers do
Pointers hold the memory address of a variable, not a copy of its value.
Step 2: Why modify original data matters
Using pointers lets functions change the actual variable, not just a copy, which is efficient and necessary in many cases.
Final Answer:
To allow functions to modify the original variable's value -> Option B
Quick Check:
Pointers let functions change original data = C [OK]
Hint: Pointers let functions change original data, not copies [OK]
Common Mistakes:
Thinking pointers create new variables
Believing pointers slow down programs
Assuming pointers avoid variables
2. Which of the following is the correct way to declare a pointer to an int in Go?
easy
A. var p *int
B. var p int*
C. var *p int
D. var p &int
Solution
Step 1: Recall Go pointer syntax
In Go, a pointer type is written as * followed by the type, e.g., *int for pointer to int.
Step 2: Match syntax to options
var p *int uses 'var p *int', which is the correct syntax for declaring a pointer to int.
Final Answer:
var p *int -> Option A
Quick Check:
Pointer declaration = var p *int [OK]
Hint: Pointer type uses * before type, like *int [OK]