Bird
Raised Fist0
Goprogramming~20 mins

Pointer receivers in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pointer Receiver Mastery
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)
}
A6
BCompilation error
C0
D5
Attempts:
2 left
💡 Hint
Think about how pointer receivers modify the original struct.
❓ Predict Output
intermediate
2: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)
}
A0
B20
C10
DCompilation error
Attempts:
2 left
💡 Hint
Consider if the method changes the original struct or a copy.
🔧 Debug
advanced
2: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)
}
AThe method should have a value receiver instead of a pointer receiver.
BThe method Rename is missing a return statement.
CThe struct field name is unexported and cannot be modified.
DThe method assigns a new pointer to i, but does not modify the original struct's fields.
Attempts:
2 left
💡 Hint
Think about what happens when you assign to the pointer receiver variable inside the method.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in pointer receiver method
Which option contains a syntax error in defining a pointer receiver method in Go?
Afunc (p Person*) SetName(name string) { p.name = name }
B} eman = eman.p { )gnirts eman(emaNteS )nosreP* p( cnuf
Cunc (p *Person) SetName(name string) { p.name = name }
Dfunc (p *Person) SetName(name string) { p.name = name }
Attempts:
2 left
💡 Hint
Check the placement of the asterisk in the receiver syntax.
🚀 Application
expert
3: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)
}
A6
B4
C3
D1
Attempts:
2 left
💡 Hint
Remember which methods modify the original struct and which do not.

Practice

(1/5)
1. What is the main benefit of using a pointer receiver in Go methods?
easy
A. It allows the method to modify the original struct data.
B. It makes the method run faster by using multiple CPU cores.
C. It automatically creates a copy of the struct for safety.
D. It prevents the method from accessing struct fields.

Solution

  1. Step 1: Understand pointer receiver purpose

    Pointer receivers let methods access and change the original struct, not a copy.
  2. Step 2: Compare with value receivers

    Value receivers get a copy, so changes inside the method don't affect the original struct.
  3. Final Answer:

    It allows the method to modify the original struct data. -> Option A
  4. Quick Check:

    Pointer receiver modifies original struct = A [OK]
Hint: Pointer receivers let methods change original data directly [OK]
Common Mistakes:
  • Thinking pointer receivers run methods on copies
  • Confusing pointer receivers with concurrency
  • Believing pointer receivers prevent access to fields
2. Which of the following is the correct syntax to define a method with a pointer receiver for struct Person?
easy
A. func (p *Person) UpdateName(name string) {}
B. func (p Person) UpdateName(name string) {}
C. func UpdateName(p Person, name string) {}
D. func UpdateName(*Person p, name string) {}

Solution

  1. Step 1: Identify pointer receiver syntax

    Pointer receiver syntax uses (p *Person) before method name.
  2. Step 2: Check each option

    func (p *Person) UpdateName(name string) {} correctly uses func (p *Person) UpdateName(name string) {}. Others either use value receiver or wrong syntax.
  3. Final Answer:

    func (p *Person) UpdateName(name string) {} -> Option A
  4. Quick Check:

    Pointer receiver syntax = (p *Person) [OK]
Hint: Pointer receiver always uses * before struct name in method receiver [OK]
Common Mistakes:
  • Using value receiver syntax instead of pointer
  • Placing * incorrectly in function parameters
  • Confusing method receiver with function parameters
3. What will be the output of this Go program?
package main
import "fmt"
type Counter struct { value int }
func (c *Counter) Increment() { c.value++ }
func main() {
 c := Counter{value: 5}
 c.Increment()
 fmt.Println(c.value)
}
medium
A. 5
B. Compilation error
C. 0
D. 6

Solution

  1. Step 1: Understand method call with pointer receiver

    The method Increment has a pointer receiver, so it modifies the original Counter struct's value.
  2. Step 2: Trace the code execution

    Initial value is 5, calling Increment() increases it by 1, so value becomes 6. Then it prints 6.
  3. Final Answer:

    6 -> Option D
  4. Quick Check:

    Pointer receiver increments original value = 6 [OK]
Hint: Pointer receiver methods change original struct values [OK]
Common Mistakes:
  • Assuming value receiver copies and does not change original
  • Expecting output 5 without increment
  • Thinking code causes compile error
4. Identify the error in this Go code snippet:
package main
import "fmt"
type Item struct { count int }
func (i Item) Increase() { i.count++ }
func main() {
 item := Item{count: 10}
 item.Increase()
 fmt.Println(item.count)
}
medium
A. Struct Item is missing a field tag.
B. fmt.Println cannot print struct fields directly.
C. Method Increase should have a pointer receiver to modify count.
D. Increase method must return the new count.

Solution

  1. Step 1: Analyze method receiver type

    The method Increase uses a value receiver, so it works on a copy of Item.
  2. Step 2: Understand effect on original struct

    Since it modifies the copy, the original item.count remains unchanged at 10.
  3. Final Answer:

    Method Increase should have a pointer receiver to modify count. -> Option C
  4. Quick Check:

    Value receiver does not change original struct = B [OK]
Hint: Use pointer receiver to modify struct fields inside methods [OK]
Common Mistakes:
  • Thinking value receiver modifies original struct
  • Believing fmt.Println can't print fields
  • Expecting method must return value to update
5. You want to create a method Reset for struct Timer that sets its seconds field to zero. Which approach correctly uses pointer receivers to ensure the original Timer is reset?
type Timer struct { seconds int }
// Choose the correct Reset method:
hard
A. func (t Timer) Reset() { t.seconds = 0 }
B. func (t *Timer) Reset() { t.seconds = 0 }
C. func Reset(t Timer) { t.seconds = 0 }
D. func Reset(t *Timer) int { t.seconds = 0; return t.seconds }

Solution

  1. Step 1: Understand method receiver for modifying struct

    To modify the original Timer, the method must have a pointer receiver (t *Timer).
  2. Step 2: Check each option's effect

    The pointer receiver func (t *Timer) Reset() { t.seconds = 0 } correctly modifies the original struct. The value receiver version works on a copy, so changes won't persist. The plain function signatures are not methods attached to the Timer type, and returning a value is unnecessary for a reset operation.
  3. Final Answer:

    func (t *Timer) Reset() { t.seconds = 0 } -> Option B
  4. Quick Check:

    Pointer receiver method resets original struct = C [OK]
Hint: Reset original struct fields with pointer receiver methods [OK]
Common Mistakes:
  • Using value receiver and expecting original change
  • Confusing functions with methods
  • Returning value unnecessarily in reset method
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