0
0
Goprogramming~20 mins

Implementing interfaces 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 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.