Bird
Raised Fist0
Goprogramming~20 mins

Value receivers in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Value 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 value 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)
}
ACompilation error
B6
C0
D5
Attempts:
2 left
💡 Hint
Think about whether the method changes the original struct or a copy.
❓ Predict Output
intermediate
2:00remaining
Effect of value receiver on struct field
What will this program print?
Go
package main
import "fmt"
type Point struct {
    x int
}
func (p Point) Move() {
    p.x = 10
}
func main() {
    pt := Point{x: 5}
    pt.Move()
    fmt.Println(pt.x)
}
A5
B10
C0
DRuntime panic
Attempts:
2 left
💡 Hint
Does the Move method change the original Point or a copy?
🔧 Debug
advanced
2:00remaining
Why does this value receiver method not update the struct?
Consider this code snippet. Why does the field not update after calling Update()? package main import "fmt" type Data struct { value int } func (d Data) Update() { d.value = 100 } func main() { data := Data{value: 50} data.Update() fmt.Println(data.value) }
ABecause Update has a value receiver and modifies a copy, not the original struct.
BBecause the struct Data is immutable in Go.
CBecause the Update method is missing a pointer receiver syntax.
DBecause the field value is unexported and cannot be changed.
Attempts:
2 left
💡 Hint
Think about how value receivers work with struct copies.
📝 Syntax
advanced
2:00remaining
Identify the error in this value receiver method
Which option correctly fixes the syntax error in this method? func (c Counter) Increment() { c.count++ } Assuming Counter is a struct with field count int.
A
func (c Counter) Increment() {
    c.count = c.count + 1
}
B
func (c *Counter) Increment() {
    c.count++
}
C
func Increment(c Counter) {
    c.count++
}
D
func (c Counter) Increment() {
    c.count += 1
}
Attempts:
2 left
💡 Hint
Which receiver type allows modifying the original struct?
🚀 Application
expert
3:00remaining
Predict the output with mixed value and pointer receivers
What is the output of this program? package main import "fmt" type Number struct { n int } func (num Number) AddOne() { num.n++ } func (num *Number) AddTwo() { num.n += 2 } func main() { x := Number{n: 1} x.AddOne() fmt.Println(x.n) x.AddTwo() fmt.Println(x.n) }
A
1
1
B
2
3
C
1
3
D
2
4
Attempts:
2 left
💡 Hint
Remember which methods modify the original struct and which modify copies.

Practice

(1/5)
1. What does a value receiver method in Go do when called on a struct?
easy
A. It requires a pointer to the struct to work.
B. It modifies the original struct directly.
C. It works on a copy of the struct, leaving the original unchanged.
D. It only works with built-in types, not structs.

Solution

  1. Step 1: Understand value receiver behavior

    Value receivers get a copy of the struct, so changes inside the method do not affect the original.
  2. Step 2: Compare with pointer receivers

    Pointer receivers work on the original struct, but value receivers do not.
  3. Final Answer:

    It works on a copy of the struct, leaving the original unchanged. -> Option C
  4. Quick Check:

    Value receiver = copy, no original change [OK]
Hint: Value receivers copy data; original stays safe [OK]
Common Mistakes:
  • Thinking value receivers modify the original struct
  • Confusing value and pointer receivers
  • Assuming value receivers require pointers
2. Which of the following is the correct syntax to define a method with a value receiver in Go?
easy
A. func greet(p Person) {}
B. func (p Person) greet() {}
C. func (p *Person) greet() {}
D. func greet(*Person) {}

Solution

  1. Step 1: Identify method receiver syntax

    Methods with receivers use the syntax: func (receiver Type) methodName() {}.
  2. Step 2: Distinguish value vs pointer receiver

    Value receiver uses the type directly (e.g., Person), pointer receiver uses *Person.
  3. Final Answer:

    func (p Person) greet() {} -> Option B
  4. Quick Check:

    Value receiver syntax = func (p Type) method() [OK]
Hint: Value receiver uses type, pointer uses *type [OK]
Common Mistakes:
  • Using pointer syntax for value receiver
  • Defining method without receiver
  • Confusing function parameter with method 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 main() {
    c := Counter{count: 5}
    c.increment()
    fmt.Println(c.count)
}
medium
A. 5
B. 6
C. 0
D. Compilation error

Solution

  1. Step 1: Analyze increment method with value receiver

    The increment method uses a value receiver, so it works on a copy of Counter, not the original.
  2. Step 2: Check effect on original Counter

    Incrementing c.count inside the method changes only the copy; original c.count remains 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Value receiver method does not change original value [OK]
Hint: Value receiver methods don't change original struct [OK]
Common Mistakes:
  • Assuming increment changes original count
  • Confusing value and pointer receivers
  • Expecting output 6 instead of 5
4. Identify the problem in this Go code snippet and fix it:
type Point struct { x, y int }
func (p Point) move(dx, dy int) {
    p.x += dx
    p.y += dy
}
func main() {
    pt := Point{1, 2}
    pt.move(3, 4)
    fmt.Println(pt)
}
medium
A. Use global variables instead of struct fields
B. Return new Point from move method
C. No fix needed; code works as intended
D. Change method receiver to pointer: func (p *Point) move(dx, dy int)

Solution

  1. Step 1: Understand why move method doesn't update original

    Method uses value receiver, so changes affect only a copy, not original Point.
  2. Step 2: Fix by using pointer receiver

    Changing receiver to *Point allows method to modify original struct fields.
  3. Final Answer:

    Change method receiver to pointer: func (p *Point) move(dx, dy int) -> Option D
  4. Quick Check:

    Pointer receiver needed to modify original struct [OK]
Hint: Use pointer receiver to modify struct fields [OK]
Common Mistakes:
  • Not using pointer receiver for modifying methods
  • Trying to fix by returning new struct without assignment
  • Ignoring that value receiver copies struct
5. You have a struct Book with a method Display() using a value receiver. You want to add a method UpdateTitle(newTitle string) that changes the title. Which is the best approach?
hard
A. Define UpdateTitle with a pointer receiver to modify the original Book.
B. Define UpdateTitle with a value receiver and assign the new title inside it.
C. Keep both methods with value receivers for consistency.
D. Make Display use a pointer receiver instead of value receiver.

Solution

  1. Step 1: Understand method receiver choice for updating data

    To change the original struct, methods must use pointer receivers.
  2. Step 2: Decide best practice for mixed methods

    It's fine to have Display use value receiver (read-only), and UpdateTitle use pointer receiver (modifies data).
  3. Final Answer:

    Define UpdateTitle with a pointer receiver to modify the original Book. -> Option A
  4. Quick Check:

    Modify method uses pointer receiver; read-only can use value [OK]
Hint: Use pointer receiver for methods that change data [OK]
Common Mistakes:
  • Using value receiver for methods that modify struct
  • Changing all methods to pointer receivers unnecessarily
  • Confusing method consistency with receiver type
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