0
0
Goprogramming~20 mins

Interface use cases in Go - Practice Problems & Coding Challenges

Choose your learning style9 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 to call a method?
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())
}
AWoof!
BCompilation error
Cspeak
DWoof
Attempts:
2 left
💡 Hint
Check the Speak method implementation and what it returns.
🧠 Conceptual
intermediate
1:30remaining
Why use interfaces in Go?
Which of the following is the main reason to use interfaces in Go?
ATo enable polymorphism and write flexible code that works with different types
BTo improve program speed by avoiding function calls
CTo declare variables with fixed memory size
DTo enforce inheritance like in other languages
Attempts:
2 left
💡 Hint
Think about how interfaces allow different types to be used interchangeably.
🔧 Debug
advanced
2:30remaining
Identify the runtime error with interface nil check
What error will this Go program produce when run?
Go
package main
import "fmt"
type Reader interface {
    Read() string
}
type File struct {}
func (f *File) Read() string {
    return "file content"
}
func main() {
    var r Reader
    var f *File = nil
    r = f
    if r == nil {
        fmt.Println("Reader is nil")
    } else {
        fmt.Println(r.Read())
    }
}
ACompilation error
Bfile content
Cpanic: runtime error: invalid memory address or nil pointer dereference
DReader is nil
Attempts:
2 left
💡 Hint
Check how interface values with nil underlying pointers behave in comparisons.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly implements an interface?
Given interface Writer with method Write(string) error, which option correctly implements it?
Go
type Writer interface {
    Write(s string) error
}
A
type File struct {}
func Write(f File, s string) error {
    return nil
}
B
type File struct {}
func (f *File) Write() error {
    return nil
}
C
type File struct {}
func (f File) Write(s int) error {
    return nil
}
D
type File struct {}
func (f File) Write(s string) error {
    return nil
}
Attempts:
2 left
💡 Hint
Check method signature matches exactly the interface method.
🚀 Application
expert
3:00remaining
How many types implement this interface?
Given the interface and types below, how many distinct types implement the interface Printable?
Go
package main
import "fmt"
type Printable interface {
    Print()
}
type A struct {}
func (a A) Print() { fmt.Println("A") }
type B struct {}
func (b *B) Print() { fmt.Println("B") }
type C struct {}
func (c C) Print() { fmt.Println("C") }
func main() {}
A2
B1
C3
D0
Attempts:
2 left
💡 Hint
Remember that in Go, types with pointer receiver methods also implement the interface via automatic address taking.