Bird
Raised Fist0
Goprogramming~20 mins

Receiver types in Go - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between a value receiver and a pointer receiver in Go methods?
easy
A. Value receivers are faster than pointer receivers in all cases.
B. Value receivers can only read data; pointer receivers can only write data.
C. Pointer receivers create a new copy of the data each time.
D. Value receivers get a copy of the data; pointer receivers get the original data to modify.

Solution

  1. Step 1: Understand receiver types

    Value receivers receive a copy of the original data, so changes inside the method do not affect the original.
  2. Step 2: Pointer receivers behavior

    Pointer receivers receive the address of the original data, allowing methods to modify the original value.
  3. Final Answer:

    Value receivers get a copy of the data; pointer receivers get the original data to modify. -> Option D
  4. Quick Check:

    Receiver type = copy vs original [OK]
Hint: Pointer receivers allow modifying original data, value receivers do not [OK]
Common Mistakes:
  • Thinking value receivers modify original data
  • Believing pointer receivers create copies
  • Assuming pointer receivers are always slower
2. Which of the following is the correct syntax to define a method with a pointer receiver for a struct Person?
easy
A. func (p Person) Greet() string { return "Hi" }
B. func (p *Person) Greet() string { return "Hi" }
C. func Greet(p *Person) string { return "Hi" }
D. func (p Person*) Greet() string { return "Hi" }

Solution

  1. Step 1: Identify pointer receiver syntax

    Pointer receivers use an asterisk before the type in the receiver: (p *Person).
  2. Step 2: Check method declaration format

    The method must be declared with receiver in parentheses before the method name.
  3. Final Answer:

    func (p *Person) Greet() string { return "Hi" } -> Option B
  4. Quick Check:

    Pointer receiver syntax = (p *Type) [OK]
Hint: Pointer receiver syntax uses * before type inside parentheses [OK]
Common Mistakes:
  • Missing parentheses around receiver
  • Placing * after variable name instead of type
  • Defining method without receiver
3. What will be the output of this Go program?
package main
import "fmt"
type Counter struct { count int }
func (c Counter) Increment() { c.count++ }
func (c *Counter) IncrementPtr() { c.count++ }
func main() {
  c := Counter{count: 5}
  c.Increment()
  fmt.Println(c.count)
  c.IncrementPtr()
  fmt.Println(c.count)
}
medium
A. 5 6
B. 5 5
C. 6 6
D. 6 7

Solution

  1. Step 1: Analyze Increment method with value receiver

    Increment uses a value receiver, so it works on a copy. The original count remains 5 after calling Increment.
  2. Step 2: Analyze IncrementPtr method with pointer receiver

    IncrementPtr uses a pointer receiver, so it modifies the original count from 5 to 6.
  3. Final Answer:

    5 6 -> Option A
  4. Quick Check:

    Value receiver no change, pointer receiver changes [OK]
Hint: Value receiver copies data; pointer receiver changes original [OK]
Common Mistakes:
  • Assuming value receiver changes original
  • Confusing method calls with pointer vs value receivers
  • Expecting both increments to change count
4. Identify the error in this Go code snippet:
type Rectangle struct { width, height int }
func (r Rectangle) Scale(factor int) {
  r.width = r.width * factor
  r.height = r.height * factor
}
func main() {
  rect := Rectangle{2, 3}
  rect.Scale(2)
  fmt.Println(rect.width, rect.height)
}
medium
A. The struct fields width and height must be pointers.
B. Scale method must return a new Rectangle instead of modifying fields.
C. Scale method should have a pointer receiver to modify Rectangle fields.
D. The Scale method should be defined outside the Rectangle type.

Solution

  1. Step 1: Check receiver type in Scale method

    Scale uses a value receiver, so it works on a copy and does not modify the original Rectangle.
  2. Step 2: Fix receiver to pointer to modify original

    Changing receiver to pointer (r *Rectangle) allows Scale to modify the original struct fields.
  3. Final Answer:

    Scale method should have a pointer receiver to modify Rectangle fields. -> Option C
  4. Quick Check:

    Modify struct fields = pointer receiver needed [OK]
Hint: Use pointer receiver to change struct fields inside methods [OK]
Common Mistakes:
  • Using value receiver expecting changes to persist
  • Trying to make struct fields pointers unnecessarily
  • Thinking methods must be outside struct
5. You want to create a Go method Reset for a struct Timer that sets its seconds field to zero. Which receiver type should you use and why?
hard
A. Use a pointer receiver because you need to modify the original Timer's seconds field.
B. Use a value receiver because it is simpler and does not modify the original.
C. Use a value receiver and return a new Timer with seconds zeroed.
D. Use a pointer receiver but do not modify any fields inside the method.

Solution

  1. Step 1: Understand the goal of Reset method

    Reset must change the Timer's seconds field to zero, modifying the original struct.
  2. Step 2: Choose receiver type to allow modification

    Pointer receiver is needed to modify the original Timer instance inside the method.
  3. Final Answer:

    Use a pointer receiver because you need to modify the original Timer's seconds field. -> Option A
  4. Quick Check:

    Modify original struct = pointer receiver [OK]
Hint: Modify original struct? Use pointer receiver [OK]
Common Mistakes:
  • Using value receiver expecting original to change
  • Returning new struct instead of modifying original
  • Using pointer receiver but not modifying fields
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard