Challenge - 5 Problems
Go Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Think about whether the method changes the original struct or a copy.
✗ Incorrect
The method Increment has a pointer receiver, so it modifies the original Counter's count field. Starting from 5, it increments to 6.
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Does the method change the original struct or a copy?
✗ Incorrect
The method Double has a value receiver, so it works on a copy of Number. The original value remains unchanged at 10.
🔧 Debug
advanced2: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) }
Attempts:
2 left
💡 Hint
Think about how methods with value receivers behave when modifying fields.
✗ Incorrect
The method SetName uses a value receiver, so it modifies a copy of Person. To change the original, the receiver must be a pointer.
🧠 Conceptual
advanced2: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()) }
Attempts:
2 left
💡 Hint
Value receiver methods belong to both value and pointer types, pointer receiver methods belong only to pointer types.
✗ Incorrect
The method with value receiver means Dog (value) implements Speaker. Pointer receiver methods only belong to *Dog. So Dog implements Speaker here.
❓ Predict Output
expert3: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()) }
Attempts:
2 left
💡 Hint
Remember that method calls depend on the type of the variable, not just the underlying data.
✗ Incorrect
c.Speak() calls Cat's Speak method (value receiver), printing "Meow, Whiskers". a.Speak() calls Animal's Speak method (pointer receiver), printing "I am Whiskers".