0
0
Goprogramming~20 mins

Receiver types in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Receiver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
}
A5
B6
C0
DCompilation error
Attempts:
2 left
💡 Hint
Think about how pointer receivers modify the original struct.
Predict Output
intermediate
2: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)
}
ACompilation error
B20
C0
D10
Attempts:
2 left
💡 Hint
Consider if the method changes the original struct or a copy.
🔧 Debug
advanced
2: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)
}
ABecause Rename uses a value receiver, it modifies a copy, not the original struct.
BBecause the field name is unexported and cannot be changed.
CBecause the method Rename is not called properly.
DBecause the struct Item is immutable in Go.
Attempts:
2 left
💡 Hint
Think about how value receivers work in Go methods.
Predict Output
advanced
2: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)
}
A25
B35
C15
DCompilation error
Attempts:
2 left
💡 Hint
Remember which methods modify the original struct and which do not.
🧠 Conceptual
expert
3:00remaining
Choosing receiver types for methods
Which statement about receiver types in Go is correct?
AValue receivers always improve performance by avoiding pointers.
BValue receivers are required to modify the original struct fields.
CPointer receivers allow methods to modify the original struct and avoid copying large structs.
DPointer receivers cannot be used with interface types.
Attempts:
2 left
💡 Hint
Think about when you want to change the original data and efficiency.