What if you could change data instantly everywhere without making copies?
Why Address and dereference operators in Go? - Purpose & Use Cases
Imagine you have a list of friends' phone numbers written on paper. To update one number, you have to find the exact paper, erase the old number, and write the new one. If you copy the list to share, changing a number in your copy won't update the original list.
Manually copying and updating values is slow and error-prone. You might update a copy but forget to update the original, causing confusion. Also, passing large data around wastes memory and time.
Address and dereference operators let you work directly with the location of data, not just copies. This means you can update the original data easily and efficiently, like having a direct phone line instead of a paper copy.
var x int = 10 var y int = x y = 20 // x stays 10
var x int = 10 var p *int = &x *p = 20 // x is now 20
It enables efficient data updates and sharing by working directly with memory addresses, avoiding unnecessary copies.
When building a game, you can pass a pointer to a player's health so multiple parts of the game can update it instantly without copying the health value everywhere.
Manual copying causes slow and error-prone updates.
Address operators get the location of data.
Dereference operators let you change the original data directly.
Practice
& operator do in Go?Solution
Step 1: Understand the
The&operator&operator is used to get the address of a variable in memory.Step 2: Differentiate from
The*operator*operator is used to access the value at an address, not to get the address itself.Final Answer:
It returns the memory address of a variable. -> Option BQuick Check:
&operator = address retrieval [OK]
- Confusing & with * operator
- Thinking & multiplies values
- Assuming & declares variables
num in Go?Solution
Step 1: Understand pointer declaration syntax
In Go, a pointer to an int is declared as*int. To assign it, use the address operator&on the variable.Step 2: Check each option
var p *int = &num correctly declarespas a pointer to int and assigns the address ofnum. Others misuse types or operators.Final Answer:
var p *int = &num -> Option CQuick Check:
Pointer declaration =var p *int = &num[OK]
- Using * on variable instead of type
- Using & with type instead of variable
- Mixing variable and type names
package main
import "fmt"
func main() {
x := 10
p := &x
*p = 20
fmt.Println(x)
}Solution
Step 1: Understand pointer assignment
Variablepstores the address ofx. Using*p = 20changes the value at that address.Step 2: Check final value of
Sincex*ppoints tox, changing*pto 20 updatesxto 20.Final Answer:
20 -> Option DQuick Check:
Dereferencing pointer changes original variable [OK]
- Thinking *p = 20 creates new variable
- Expecting print to show address
- Confusing & and * operators
func main() {
var a int = 5
var p *int
*p = a
fmt.Println(*p)
}Solution
Step 1: Check pointer initialization
Pointerpis declared but not assigned an address before dereferencing with*p = a.Step 2: Understand consequences
Dereferencing an uninitialized pointer causes a runtime panic (nil pointer dereference).Final Answer:
Pointer p is not initialized before dereferencing. -> Option AQuick Check:
Dereference only initialized pointers [OK]
- Dereferencing nil pointer
- Confusing pointer and value assignment
- Assuming fmt.Println can't print pointers
package main
import "fmt"
func updateValue(p *int) {
*p = *p + 5
}
func main() {
val := 10
updateValue(&val)
fmt.Println(val)
}Solution
Step 1: Understand function parameter
The functionupdateValuetakes a pointer to an int. Passing&valsends the address ofval.Step 2: Analyze pointer dereference and update
Inside the function,*p = *p + 5adds 5 to the value at the address, updatingvalto 15.Final Answer:
15 -> Option AQuick Check:
Pointer parameter updates original variable [OK]
- Expecting original val unchanged
- Confusing pointer with value copy
- Thinking function returns new value
Ready to prove it?
Pass the test and this pattern moves unknown
