Challenge - 5 Problems
Go Receiver Master
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
Value receiver method effect
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 Double method has a value receiver, so it works on a copy of num. The original num.value remains 10.
🔧 Debug
advanced2:30remaining
Why does this method not modify the struct?
Given this code, why does the method not change the struct's field?
Go
package main import "fmt" type Item struct { name string } func (i Item) Rename(newName string) { i.name = newName } func main() { it := Item{name: "Book"} it.Rename("Notebook") fmt.Println(it.name) }
Attempts:
2 left
💡 Hint
Think about how value receivers work in Go methods.
✗ Incorrect
Methods with value receivers get a copy of the struct. Changing fields on the copy does not affect the original struct.
❓ Predict Output
advanced2:30remaining
Output with mixed receiver types
What is the output of this Go program?
Go
package main import "fmt" type Box struct { size int } func (b Box) Grow() { b.size += 10 } func (b *Box) Expand() { b.size += 20 } func main() { b := Box{size: 5} b.Grow() b.Expand() fmt.Println(b.size) }
Attempts:
2 left
💡 Hint
Remember which methods modify the original struct and which do not.
✗ Incorrect
Grow has a value receiver and does not change the original size (still 5). Expand has a pointer receiver and adds 20, so final size is 25.
🧠 Conceptual
expert3:00remaining
Choosing receiver types for methods
Which statement about receiver types in Go is correct?
Attempts:
2 left
💡 Hint
Think about when you want to change the original data and efficiency.
✗ Incorrect
Pointer receivers let methods change the original struct and avoid copying large structs. Value receivers work on copies and cannot modify the original.