Bird
Raised Fist0
Goprogramming~20 mins

Defining methods in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Methods 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)
}
A0
B5
C6
DCompilation error
Attempts:
2 left
💡 Hint
Think about whether the method changes the original struct or a copy.
❓ Predict Output
intermediate
2:00remaining
Output of method with value receiver
What will this Go program print?
Go
package main
import "fmt"
type Number struct {
    value int
}
func (n Number) Double() {
    n.value = n.value * 2
}
func main() {
    num := Number{value: 10}
    num.Double()
    fmt.Println(num.value)
}
A10
B20
C0
DCompilation error
Attempts:
2 left
💡 Hint
Does the method change the original struct or a copy?
🔧 Debug
advanced
2:00remaining
Why does this setter method not update the struct?
This Go code does not update the name. What is the cause of the error?
Go
package main
import "fmt"
type Person struct {
    name string
}
func (p Person) SetName(newName string) {
    p.name = newName
}
func main() {
    p := Person{name: "Alice"}
    p.SetName("Bob")
    fmt.Println(p.name)
}
ACannot assign to p.name because p is a value receiver and method cannot modify it
BThe method SetName must return the new name to update p.name
Cfmt.Println cannot access p.name because it is unexported
DMissing pointer receiver in SetName method to modify original struct
Attempts:
2 left
💡 Hint
Think about how methods with value receivers behave when modifying fields.
🧠 Conceptual
advanced
2:00remaining
Method sets and interface implementation
Given the interface and struct below, which method set allows the struct to implement the interface?
Go
package main
import "fmt"
type Speaker interface {
    Speak() string
}
type Dog struct {}
func (d Dog) Speak() string {
    return "Woof!"
}
func (d *Dog) Speak() string {
    return "Bark!"
}
func main() {
    var s Speaker
    d := Dog{}
    s = d
    fmt.Println(s.Speak())
}
ANeither Dog nor *Dog implement Speaker because of method name conflict
BDog implements Speaker because it has Speak() with value receiver
CBoth Dog and *Dog implement Speaker interface
D*Dog implements Speaker because it has Speak() with pointer receiver
Attempts:
2 left
💡 Hint
Value receiver methods belong to both value and pointer types, pointer receiver methods belong only to pointer types.
❓ Predict Output
expert
3:00remaining
Output of method with embedded struct and pointer receiver
What is the output of this Go program?
Go
package main
import "fmt"
type Animal struct {
    name string
}
func (a *Animal) Speak() string {
    return "I am " + a.name
}
type Cat struct {
    Animal
}
func (c Cat) Speak() string {
    return "Meow, " + c.name
}
func main() {
    c := Cat{Animal{name: "Whiskers"}}
    var a *Animal = &c.Animal
    fmt.Println(c.Speak())
    fmt.Println(a.Speak())
}
A
Meow, Whiskers
I am Whiskers
B
Meow, Whiskers
Meow, Whiskers
C
I am Whiskers
Meow, Whiskers
DCompilation error due to method conflict
Attempts:
2 left
💡 Hint
Remember that method calls depend on the type of the variable, not just the underlying data.

Practice

(1/5)
1. What is the purpose of defining a method with a receiver in Go?
easy
A. To import external packages into the program
B. To create a standalone function unrelated to any type
C. To associate a function with a specific type so it can act on its data
D. To declare a global variable accessible everywhere

Solution

  1. Step 1: Understand what a receiver does

    A receiver connects a method to a type, allowing the method to access or modify that type's data.
  2. Step 2: Compare with other options

    Options A, B, and C describe unrelated concepts: imports, standalone functions, and global variables.
  3. Final Answer:

    To associate a function with a specific type so it can act on its data -> Option C
  4. Quick Check:

    Receiver links method to type = D [OK]
Hint: Receiver connects method to type's data [OK]
Common Mistakes:
  • Thinking methods are standalone functions
  • Confusing methods with variables
  • Believing receivers import packages
2. Which of the following is the correct syntax to define a method named Speak for a struct type Dog?
easy
A. func (d Dog) Speak() { }
B. func Speak(d Dog) { }
C. func Dog.Speak() { }
D. func Speak() Dog { }

Solution

  1. Step 1: Recall Go method syntax

    Methods use the form: func (receiver Type) MethodName() { }.
  2. Step 2: Match syntax to options

    func (d Dog) Speak() { } matches this pattern exactly. func Speak(d Dog) { } is a function, not a method. func Dog.Speak() { } is invalid syntax. func Speak() Dog { } is a function returning Dog, not a method.
  3. Final Answer:

    func (d Dog) Speak() { } -> Option A
  4. Quick Check:

    Method syntax = func (receiver Type) Name() [OK]
Hint: Method syntax always has receiver in parentheses before name [OK]
Common Mistakes:
  • Omitting receiver parentheses
  • Using dot notation in function name
  • Confusing methods with functions
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. 6
B. 5
C. 0
D. Compilation error

Solution

  1. Step 1: Understand receiver type in Increment method

    The method uses a value receiver (c Counter), so it works on a copy of the struct, not the original.
  2. Step 2: Trace the method call and print

    Increment increases the copy's value, but original c.value remains 5. So printing c.value outputs 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Value receiver does not modify original = 5 [OK]
Hint: Value receiver copies data; original stays unchanged [OK]
Common Mistakes:
  • Assuming value receiver changes original data
  • Confusing pointer and value receivers
  • Expecting incremented output
4. Identify the error in this method definition:
package main
import "fmt"
type Person struct { name string }
func (p *Person) SetName(newName string) {
    p.name = newName
}
func main() {
    p := Person{name: "Alice"}
    p.SetName("Bob")
    fmt.Println(p.name)
}
medium
A. No error; code runs and prints 'Bob'
B. Cannot call method with pointer receiver on value variable
C. Method must return a value
D. Missing import of fmt package

Solution

  1. Step 1: Check pointer receiver usage

    In Go, methods with pointer receivers can be called on value variables; Go automatically takes address.
  2. Step 2: Verify imports and method behavior

    Code imports fmt, method sets name correctly, so p.name becomes "Bob" and prints it.
  3. Final Answer:

    No error; code runs and prints 'Bob' -> Option A
  4. Quick Check:

    Pointer receiver works on value variable = B [OK]
Hint: Pointer receiver methods can be called on values directly [OK]
Common Mistakes:
  • Thinking pointer receiver requires pointer variable
  • Forgetting Go auto-addressing
  • Expecting compile error
5. You want to define a method Reset for a struct Timer that sets its seconds field to zero. Which receiver type should you use and why? Options: A) Pointer receiver, because it modifies the original struct's field B) Value receiver, because it copies the struct and resets the copy C) Value receiver, because methods cannot have pointer receivers D) Pointer receiver, but it cannot modify fields inside the struct
hard
A. Pointer receiver, but it cannot modify fields inside the struct
B. Value receiver, because it copies the struct and resets the copy
C. Value receiver, because methods cannot have pointer receivers
D. Pointer receiver, because it modifies the original struct's field

Solution

  1. Step 1: Understand effect of receiver types on struct modification

    Value receivers work on copies, so changes do not affect original struct fields.
  2. Step 2: Choose pointer receiver to modify original

    Pointer receivers allow methods to change the original struct's fields, so Reset can set seconds to zero effectively.
  3. Final Answer:

    Pointer receiver, because it modifies the original struct's field -> Option D
  4. Quick Check:

    Pointer receiver modifies original struct = A [OK]
Hint: Use pointer receiver to change original struct fields [OK]
Common Mistakes:
  • Using value receiver expecting original change
  • Believing methods can't have pointer receivers
  • Thinking pointer receiver can't modify 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