Passing values copies the data, while passing pointers shares the original data. This helps control if changes affect the original or just a copy.
Passing values vs pointers in Go
func functionName(valueType value) {
// code using value
}
func functionName(pointerType *valueType) {
// code using pointer
}Passing by value copies the data into the function.
Passing by pointer sends the address, so the function can change the original data.
func updateValue(x int) { x = 10 } func updatePointer(x *int) { *x = 10 }
var a int = 5 updateValue(a) // a is still 5 updatePointer(&a) // a is now 10
This program shows the difference between passing a value and passing a pointer. The first function call does not change the original variable. The second function call changes it.
package main import "fmt" func changeValue(val int) { val = 100 } func changePointer(ptr *int) { *ptr = 100 } func main() { a := 50 fmt.Println("Before changeValue:", a) changeValue(a) fmt.Println("After changeValue:", a) fmt.Println("Before changePointer:", a) changePointer(&a) fmt.Println("After changePointer:", a) }
Use pointers when you want to modify the original data inside a function.
Passing large structs by pointer can improve performance by avoiding copying.
Be careful with pointers to avoid unexpected changes or nil pointer errors.
Passing by value copies data; changes inside function do not affect original.
Passing by pointer shares the original data; changes inside function affect original.
Choose based on whether you want to protect or modify the original data.
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
