Pointers let you share and change data directly inside functions without copying it. This helps save memory and lets functions update values outside their own space.
Pointer behavior in functions in Go
func functionName(param *Type) {
// use *param to access or change the value
}The * before a type means the parameter is a pointer to that type.
Use *param inside the function to get or set the actual value.
func updateValue(x *int) { *x = 10 }
func printValue(x *int) {
fmt.Println(*x)
}This program shows how a function can change a variable's value using a pointer. We pass the address of number to changeNumber. Inside the function, the value at that address is changed to 42. The change is visible outside the function.
package main import "fmt" func changeNumber(num *int) { *num = 42 } func main() { number := 5 fmt.Println("Before:", number) changeNumber(&number) fmt.Println("After:", number) }
Passing a pointer means the function can change the original variable.
Use & to get the address of a variable when calling the function.
Use * inside the function to access or modify the value the pointer points to.
Pointers let functions change variables outside their own scope.
Use & to send a variable's address, and * to access or change the value inside the function.
This helps save memory and allows direct updates to data.
Practice
Solution
Step 1: Understand pointer passing
Passing a pointer sends the address of the variable, not a copy.Step 2: Effect on original variable
Inside the function, using the pointer lets you change the original variable's value.Final Answer:
Modify the original variable's value inside the function -> Option CQuick Check:
Pointer lets function change original variable [OK]
- Thinking pointers create copies instead of referencing original
- Confusing pointer with value passing
- Assuming pointers prevent modification
Solution
Step 1: Recall pointer syntax in function parameters
In Go, a pointer parameter is declared with * before the type, like *int.Step 2: Check each option
func updateValue(val *int) {} correctly uses *int as the parameter type.Final Answer:
func updateValue(val *int) {} -> Option AQuick Check:
Pointer parameter syntax = *type [OK]
- Placing * before variable name instead of type
- Using & in parameter declaration
- Omitting * for pointer type
package main
import "fmt"
func change(val *int) {
*val = 10
}
func main() {
x := 5
change(&x)
fmt.Println(x)
}Solution
Step 1: Understand pointer usage in change function
The function receives a pointer to x and sets the value at that address to 10.Step 2: Trace main function execution
x starts as 5, change(&x) updates x to 10, then fmt.Println prints x.Final Answer:
10 -> Option BQuick Check:
Pointer lets function update x to 10 [OK]
- Expecting original value 5 to print
- Confusing pointer dereference syntax
- Thinking function works without & operator
func update(val *int) {
val = 20
}
func main() {
x := 5
update(&x)
fmt.Println(x)
}Solution
Step 1: Analyze assignment inside update function
val is a pointer to int, but val = 20 tries to assign int to pointer variable, which is invalid.Step 2: Correct way to change value via pointer
Use *val = 20 to assign 20 to the value pointed by val.Final Answer:
Cannot assign int to pointer variable directly -> Option AQuick Check:
Assign to *val, not val [OK]
- Assigning value directly to pointer variable
- Forgetting to dereference pointer
- Confusing pointer and value types
package main
import "fmt"
func swap(a, b *int) {
*a, *b = *b, *a
}
func main() {
x, y := 1, 2
swap(&x, &y)
fmt.Println(x, y)
}Solution
Step 1: Understand swap function behavior
swap takes pointers to two ints and swaps their values using pointer dereferencing.Step 2: Trace main function execution
x=1, y=2 initially; swap(&x, &y) swaps values, so x becomes 2 and y becomes 1.Final Answer:
2 1 -> Option DQuick Check:
Pointer swap exchanges values correctly [OK]
- Expecting original order after swap
- Confusing pointer addresses with values
- Not using * to dereference pointers
Ready to prove it?
Pass the test and this pattern moves unknown
