Challenge - 5 Problems
Pointer Receiver Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of method with pointer receiver
What is the output of this Go program?
Go
package main import "fmt" type Counter struct { count int } func (c *Counter) Increment() { c.count++ } func main() { c := Counter{count: 5} c.Increment() fmt.Println(c.count) }
Attempts:
2 left
💡 Hint
Think about how pointer receivers modify the original struct.
✗ Incorrect
The method Increment has a pointer receiver, so it modifies the original Counter instance. Starting from 5, Increment adds 1, so the output is 6.
❓ Predict Output
intermediate2:00remaining
Effect of value receiver on struct modification
What will be printed by this Go program?
Go
package main import "fmt" type Number struct { value int } func (n Number) Double() { n.value *= 2 } func main() { num := Number{value: 10} num.Double() fmt.Println(num.value) }
Attempts:
2 left
💡 Hint
Consider if the method changes the original struct or a copy.
✗ Incorrect
The method Double has a value receiver, so it works on a copy of num. The original num.value remains 10.
🔧 Debug
advanced2:30remaining
Why does this pointer receiver method not update the struct?
This Go code intends to update the field of a struct using a pointer receiver method, but it does not work as expected. What is the reason?
Go
package main import "fmt" type Item struct { name string } func (i *Item) Rename(newName string) { i = &Item{name: newName} } func main() { it := Item{name: "Old"} it.Rename("New") fmt.Println(it.name) }
Attempts:
2 left
💡 Hint
Think about what happens when you assign to the pointer receiver variable inside the method.
✗ Incorrect
Assigning a new pointer to i inside Rename only changes the local copy of the pointer. It does not affect the original struct. To update the struct, modify its fields directly.
📝 Syntax
advanced2:00remaining
Identify the syntax error in pointer receiver method
Which option contains a syntax error in defining a pointer receiver method in Go?
Attempts:
2 left
💡 Hint
Check the placement of the asterisk in the receiver syntax.
✗ Incorrect
In Go, the pointer receiver syntax is (p *Type), not (p Type*). Option A has the asterisk after the type, which is invalid syntax.
🚀 Application
expert3:00remaining
Determine the final value after method calls with mixed receivers
Given the following Go code, what is the final value printed?
Go
package main import "fmt" type Counter struct { value int } func (c Counter) Add(n int) { c.value += n } func (c *Counter) AddPtr(n int) { c.value += n } func main() { c := Counter{value: 1} c.Add(2) c.AddPtr(3) fmt.Println(c.value) }
Attempts:
2 left
💡 Hint
Remember which methods modify the original struct and which do not.
✗ Incorrect
Add uses a value receiver, so it works on a copy and does not change c.value. AddPtr uses a pointer receiver and adds 3 to the original c.value (1 + 3 = 4). So final value is 4.