0
0
Goprogramming~20 mins

Why interfaces are used in Go - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interface Mastery in Go
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of Interfaces in Go
Why do programmers use interfaces in Go?
ATo define a set of method signatures that types can implement, enabling polymorphism and flexible code design.
BTo store data permanently on disk for later retrieval.
CTo create variables that can only hold integer values.
DTo directly manipulate memory addresses for performance.
Attempts:
2 left
💡 Hint
Think about how interfaces help different types work together by sharing common behaviors.
Predict Output
intermediate
2:00remaining
Output of Interface Usage Example
What is the output of this Go program using interfaces?
Go
package main
import "fmt"
type Speaker interface {
    Speak() string
}
type Dog struct {}
func (d Dog) Speak() string {
    return "Woof!"
}
type Cat struct {}
func (c Cat) Speak() string {
    return "Meow!"
}
func saySomething(s Speaker) {
    fmt.Println(s.Speak())
}
func main() {
    d := Dog{}
    c := Cat{}
    saySomething(d)
    saySomething(c)
}
A
Woof!
Meow!
B
Woof!
Woof!
C
Meow!
Woof!
DCompilation error due to missing method implementation
Attempts:
2 left
💡 Hint
Each struct implements Speak differently; the function prints each result.
🔧 Debug
advanced
2:00remaining
Identify the Interface Implementation Error
What error will this Go code produce and why?
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
    r = File{}
    fmt.Println(r.Read())
    r = nil
    fmt.Println(r.Read())
}
A
Output:
file content
panic: interface conversion error
BCompilation error: cannot assign nil to interface variable
Cpanic: runtime error: invalid memory address or nil pointer dereference
D
Output:
file content
<empty string>
Attempts:
2 left
💡 Hint
Consider what happens when calling a method on a nil interface value.
📝 Syntax
advanced
2:00remaining
Which Option Correctly Defines an Interface?
Which of the following Go code snippets correctly defines an interface named 'Mover' with a method 'Move() string'?
Atype Mover interface Move() string
B
interface Mover {
    Move() string
}
C
type Mover struct {
    Move() string
}
D
type Mover interface {
    Move() string
}
Attempts:
2 left
💡 Hint
Remember the Go syntax for defining interfaces uses 'type' and 'interface' keywords.
🚀 Application
expert
3:00remaining
Using Interfaces for Flexible Function Arguments
Given these types and interface, which option correctly calls the function 'process' with different types implementing the 'Processor' interface?
Go
package main
import "fmt"
type Processor interface {
    Process() string
}
type Image struct {}
func (i Image) Process() string {
    return "Processing image"
}
type Text struct {}
func (t Text) Process() string {
    return "Processing text"
}
func process(p Processor) {
    fmt.Println(p.Process())
}
func main() {
    img := Image{}
    txt := Text{}
    // Which call is correct here?
}
A
process(&img)
process(&txt)
B
process(img)
process(txt)
C
process(img)
process(&txt)
D
process(&img)
process(txt)
Attempts:
2 left
💡 Hint
Check if the Process method has pointer or value receiver and match the argument accordingly.