0
0
Goprogramming~10 mins

Interface use cases in Go - Interactive Code Practice

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]
}
Drag options to blanks, or click blank then click option'
ASpeak(string)
Bspeak()
CSpeak
DSpeak()
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses in the method declaration.
Using lowercase method names when uppercase is expected.
2fill in blank
medium

Complete the code to make type Dog implement the Speaker interface.

Go
type Dog struct {}

func (d Dog) [1] {
	fmt.Println("Woof!")
}
Drag options to blanks, or click blank then click option'
ASpeak
BSpeak()
Cspeak()
DBark()
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the interface requires.
Omitting parentheses in the method declaration.
3fill in blank
hard

Fix the error in the code to assign a Dog to a Speaker interface variable.

Go
var s Speaker
var d Dog
s = [1]
Drag options to blanks, or click blank then click option'
A*d
B&d
Cd
DSpeaker(d)
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a pointer when the method has a value receiver.
Trying to cast the type unnecessarily.
4fill in blank
hard

Fill both blanks to create a map of string keys to Speaker interface values, and add a Dog instance.

Go
var animals = map[string][1]{}

animals["dog"] = [2]
Drag options to blanks, or click blank then click option'
ASpeaker
BDog{}
CAnimal{}
DSpeaker{}
Attempts:
3 left
💡 Hint
Common Mistakes
Using a concrete type instead of the interface as map value.
Trying to assign an interface literal which is invalid.
5fill in blank
hard

Fill all three blanks to define an interface, implement it with a Cat struct, and call the method.

Go
type [1] interface {
	[2]()
}

type Cat struct {}

func (c Cat) [2]() {
	fmt.Println("Meow!")
}

func main() {
	var s [1] = Cat{}
	s.[2]()
}
Drag options to blanks, or click blank then click option'
ASpeaker
BSpeak
CSpeak()
DAnimal
Attempts:
3 left
💡 Hint
Common Mistakes
Including parentheses in blanks where only method names are expected.
Using different names for interface and method.