What if you could change something once and have it update everywhere instantly?
Passing values vs pointers in Go - When to Use Which
Imagine you have a list of friends' names written on paper. You want to share this list with a friend, but instead of giving them the paper, you make a copy and hand it over. Now, if your friend changes something on their copy, your original list stays the same. But what if you wanted your friend to update your original list directly?
Copying the entire list every time wastes paper and time. Also, if your friend changes their copy, your original list remains unchanged, causing confusion. Manually keeping track of which copy is the original and which is a duplicate is error-prone and slow.
Using pointers is like giving your friend the exact address of your original list instead of a copy. They can directly update the original list, saving time and avoiding mistakes. This way, changes are shared instantly without making multiple copies.
func updateName(name string) {
name = "NewName"
}
func main() {
original := "OldName"
updateName(original)
// original is still "OldName"
}func updateName(name *string) {
*name = "NewName"
}
func main() {
original := "OldName"
updateName(&original)
// original is now "NewName"
}It enables efficient and direct updates to data without unnecessary copying, making programs faster and easier to manage.
When editing a shared document online, instead of sending copies back and forth, everyone works on the same file location, so changes appear instantly for all.
Passing by value creates copies, which can be slow and cause outdated data.
Passing by pointer shares the original data, allowing direct updates.
Using pointers helps write efficient and clear programs.
Practice
Solution
Step 1: Understand passing by value
Passing by value means the function gets a copy of the variable, not the original.Step 2: Effect of changes inside the function
Changes to the copy do not affect the original variable outside the function.Final Answer:
The function receives a copy; changes inside do not affect the original variable. -> Option AQuick Check:
Passing by value = copy, no original change [OK]
- Thinking changes inside function affect original variable
- Confusing pointers with values
- Assuming function cannot modify any data
Solution
Step 1: Recall pointer syntax in Go function parameters
In Go, a pointer parameter is declared by placing * before the type, e.g., *int.Step 2: Identify correct syntax
func updateValue(val *int) {} is correct syntax, with * before the type. func updateValue(val int) {} declares a value parameter. func updateValue(&val int) {} and func updateValue(*val int) {} have invalid syntax.Final Answer:
func updateValue(val *int) {} -> Option AQuick Check:
Pointer parameter syntax = *type [OK]
- Putting * before variable name instead of type
- Using & in parameter declaration
- Confusing pointer syntax with value parameter
package main
import "fmt"
func changeValue(val int) {
val = 10
}
func main() {
x := 5
changeValue(x)
fmt.Println(x)
}Solution
Step 1: Analyze function parameter passing
The function changeValue receives x by value, so val is a copy of x.Step 2: Check if original variable changes
Inside changeValue, val is set to 10, but this does not affect x in main.Final Answer:
5 -> Option BQuick Check:
Passing by value means original x stays 5 [OK]
- Assuming x changes to 10 after function call
- Confusing pointer and value passing
- Expecting compilation error due to assignment
func increment(num *int) {
num = num + 1
}
func main() {
x := 5
increment(&x)
fmt.Println(x)
}Solution
Step 1: Understand pointer arithmetic in Go
Go does not support arithmetic on pointers like 'num + 1'. You must dereference the pointer to change the value.Step 2: Correct way to increment value pointed by pointer
Use '*num = *num + 1' to increment the value at the pointer address.Final Answer:
Cannot add 1 to a pointer directly; need to dereference first. -> Option DQuick Check:
Pointer arithmetic invalid; dereference to modify [OK]
- Trying to add directly to pointer variable
- Forgetting to dereference pointer
- Confusing pointer and value types
Solution
Step 1: Understand how to modify original variable
Passing by value copies the variable, so changes inside function won't affect original.Step 2: Use pointer to modify original variable
Passing a pointer allows the function to access and change the original variable's value via dereferencing.Step 3: Consider alternatives
Returning doubled value requires assignment outside; using global variables is bad practice.Final Answer:
Pass a pointer to the integer and modify the value via dereferencing. -> Option CQuick Check:
Pointer passing modifies original variable [OK]
- Assuming passing by value changes original variable
- Relying on return value without assignment
- Using global variables unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
