Bird
Raised Fist0
Goprogramming~20 mins

Implementing interfaces in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of interface method call
What is the output of this Go program that uses an interface and a struct implementing it?
Go
package main
import "fmt"

type Speaker interface {
    Speak() string
}

type Dog struct {}

func (d Dog) Speak() string {
    return "Woof!"
}

func main() {
    var s Speaker = Dog{}
    fmt.Println(s.Speak())
}
Aspeak
BWoof
CWoof!
DCompilation error
Attempts:
2 left
💡 Hint
Check the Speak method's return value and how it's printed.
❓ Predict Output
intermediate
2:00remaining
Interface value nil check
What will be printed by this Go program that checks if an interface value is nil?
Go
package main
import "fmt"

type Reader interface {
    Read() string
}

type File struct {}

func (f *File) Read() string {
    return "data"
}

func main() {
    var r Reader
    var f *File = nil
    r = f
    if r == nil {
        fmt.Println("r is nil")
    } else {
        fmt.Println("r is not nil")
    }
}
Ar is nil
Br is not nil
CCompilation error
DRuntime panic
Attempts:
2 left
💡 Hint
An interface value is nil only if both type and value are nil.
🔧 Debug
advanced
2:00remaining
Why does this interface implementation fail?
This Go code tries to assign a struct to an interface variable but fails to compile. Why?
Go
package main
import "fmt"

type Walker interface {
    Walk()
}

type Person struct {}

func (p Person) walk() {
    fmt.Println("Walking")
}

func main() {
    var w Walker = Person{}
    w.Walk()
}
ACompilation error: Person does not implement Walker (missing Walk method)
BCompilation error: cannot assign Person to Walker because of pointer receiver
CRuntime panic: method walk not found
DProgram runs and prints 'Walking'
Attempts:
2 left
💡 Hint
Check method names and case sensitivity in Go.
📝 Syntax
advanced
2:00remaining
Which option correctly implements this interface?
Given this interface, which option correctly implements it without syntax errors?
Go
type Printer interface {
    Print(msg string) string
}
A
type MyPrinter struct {}
func (p MyPrinter) Print() string {
    return "done"
}
B
type MyPrinter struct {}
func (p *MyPrinter) Print(msg string) {
    fmt.Println(msg)
}
C
type MyPrinter struct {}
func Print(msg string) string {
    return msg
}
D
type MyPrinter struct {}
func (p MyPrinter) Print(msg string) string {
    return msg
}
Attempts:
2 left
💡 Hint
Check method signature matches interface exactly.
🚀 Application
expert
2:00remaining
How many items in the slice after interface assignment?
Consider this Go code. How many elements does the slice 'items' contain after execution?
Go
package main
import "fmt"

type Item interface {
    ID() int
}

type Product struct {
    id int
}

func (p Product) ID() int {
    return p.id
}

func main() {
    var items []Item
    p1 := Product{id: 1}
    p2 := Product{id: 2}
    items = append(items, p1)
    items = append(items, &p2)
    fmt.Println(len(items))
}
A2
B1
C0
DCompilation error
Attempts:
2 left
💡 Hint
Both Product value and pointer implement Item interface.

Practice

(1/5)
1. What does it mean for a type in Go to implement an interface?
easy
A. The type can only have one method.
B. The type inherits from the interface.
C. The type must be explicitly declared as implementing the interface.
D. The type has all the methods declared in the interface.

Solution

  1. Step 1: Understand interface implementation in Go

    In Go, a type implements an interface by having all the methods the interface declares.
  2. Step 2: Compare options with Go's rules

    Go does not use inheritance or explicit declarations for interfaces; it uses method sets.
  3. Final Answer:

    The type has all the methods declared in the interface. -> Option D
  4. Quick Check:

    Interface implementation = method set match [OK]
Hint: Check if all interface methods exist in the type [OK]
Common Mistakes:
  • Thinking Go uses inheritance for interfaces
  • Believing explicit declaration is needed
  • Confusing interfaces with structs
2. Which of the following is the correct way to declare that a type implements an interface in Go?
easy
A. type MyType struct implements MyInterface {}
B. type MyType struct { MyInterface }
C. No explicit declaration needed; implement all interface methods
D. func (m MyType) implements() MyInterface {}

Solution

  1. Step 1: Recall Go interface implementation syntax

    Go does not require explicit declarations; a type implements an interface by having all its methods.
  2. Step 2: Analyze each option

    Options A, B, and D show invalid or non-Go syntax; only C correctly states Go's implicit implementation.
  3. Final Answer:

    No explicit declaration needed; implement all interface methods -> Option C
  4. Quick Check:

    Implicit implementation = No explicit declaration needed; implement all interface methods [OK]
Hint: Remember: Go uses implicit interface implementation [OK]
Common Mistakes:
  • Trying to declare implementation explicitly
  • Using inheritance syntax from other languages
  • Adding interface as a struct field
3. What will be the output of the following Go code?
package main
import "fmt"
type Speaker interface {
    Speak() string
}
type Dog struct {}
func (d Dog) Speak() string {
    return "Woof!"
}
func saySomething(s Speaker) {
    fmt.Println(s.Speak())
}
func main() {
    var d Dog
    saySomething(d)
}
medium
A. Woof!
B. Compilation error: Dog does not implement Speaker
C. Runtime error
D. No output

Solution

  1. Step 1: Check if Dog implements Speaker

    Dog has a method Speak() string, so it implements Speaker interface.
  2. Step 2: Trace the function call saySomething(d)

    saySomething calls s.Speak(), which returns "Woof!" and prints it.
  3. Final Answer:

    Woof! -> Option A
  4. Quick Check:

    Interface method call prints "Woof!" [OK]
Hint: Check if method signatures match interface exactly [OK]
Common Mistakes:
  • Assuming explicit declaration is needed
  • Confusing method receiver types
  • Expecting runtime errors without reason
4. Identify the error in this Go code that tries to implement an interface:
package main
import "fmt"
type Runner interface {
    Run()
}
type Person struct {}
func (p *Person) Run() {
    fmt.Println("Running")
}
func main() {
    var r Runner
    p := Person{}
    r = p
    r.Run()
}
medium
A. Missing Run() method in Person
B. Cannot assign Person value to Runner interface because Run() has pointer receiver
C. Interface Runner must have a return type
D. No error, code runs fine

Solution

  1. Step 1: Check method receiver type

    Run() is defined with pointer receiver (*Person), so only *Person implements Runner.
  2. Step 2: Analyze assignment r = p

    Variable p is a Person value, not *Person pointer, so it does not implement Runner.
  3. Final Answer:

    Cannot assign Person value to Runner interface because Run() has pointer receiver -> Option B
  4. Quick Check:

    Pointer receiver method requires pointer type [OK]
Hint: Match method receiver type with assigned variable type [OK]
Common Mistakes:
  • Ignoring pointer vs value receiver difference
  • Assuming interface methods must return values
  • Thinking interface assignment always works
5. Given these interfaces and types, which assignment is valid in Go?
type Reader interface {
    Read() string
}
type Writer interface {
    Write(string)
}
type ReadWriter interface {
    Reader
    Writer
}
type File struct {}
func (f *File) Read() string {
    return "data"
}
func (f *File) Write(s string) {
    // write data
}
func main() {
    var rw ReadWriter
    var f File
    // Which assignment is valid?
}
hard
A. rw = &f
B. rw = f
C. rw = File{} (without methods)
D. rw = nil

Solution

  1. Step 1: Check if File implements ReadWriter

    File has Read() and Write(string) with pointer receivers (*File), so only *File implements ReadWriter; File value does not.
  2. Step 2: Analyze assignment options

    rw = &f is valid (&f is *File). rw = f fails (File doesn't implement). C invalid. rw = nil compiles but doesn't hold usable ReadWriter.
  3. Final Answer:

    rw = &f -> Option A
  4. Quick Check:

    Pointer receiver requires pointer type [OK]
Hint: Pointer receiver methods mean only pointer type implements interface [OK]
Common Mistakes:
  • Thinking value type implements pointer receiver interface
  • Assigning nil to interface without initialization
  • Confusing interface embedding with inheritance
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