0
0
Goprogramming~10 mins

Why interfaces are used in Go - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an interface named Speaker.

Go
type Speaker interface { [1]() string }
Drag options to blanks, or click blank then click option'
ASpeak
BRun
CWalk
DJump
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated method names like Run or Walk in the Speaker interface.
2fill in blank
medium

Complete the code to implement the Speaker interface for type Person.

Go
func (p Person) [1]() string { return "Hello" }
Drag options to blanks, or click blank then click option'
ARun
BJump
CSpeak
DWalk
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name that does not match the interface.
3fill in blank
hard

Fix the error in the code to assign a Person to a Speaker variable.

Go
var s Speaker = [1]
Drag options to blanks, or click blank then click option'
Aint{}
BSpeaker{}
Cstring{}
DPerson{}
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to assign a type that does not implement Speaker.
4fill in blank
hard

Fill both blanks to create a function that accepts any Speaker and calls Speak.

Go
func greet(s [1]) string { return s.[2]() }
Drag options to blanks, or click blank then click option'
ASpeaker
BPerson
CSpeak
DHello
Attempts:
3 left
💡 Hint
Common Mistakes
Using concrete type Person instead of interface Speaker in the function parameter.
Calling a method name not defined in the interface.
5fill in blank
hard

Fill all three blanks to create a map of Speakers and call Speak on each.

Go
speakers := map[string][1]{
	"alice": [2]{},
}
for name, sp := range speakers {
	println(name, sp.[3]())
}
Drag options to blanks, or click blank then click option'
ASpeaker
BPerson
CSpeak
DHello
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong map value type.
Using wrong struct type for the map value.
Calling a method not defined in the interface.