Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare an interface named Speaker.
Go
type Speaker interface { [1]() string } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated method names like Run or Walk in the Speaker interface.
✗ Incorrect
The interface method name should be Speak to define a Speaker interface.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name that does not match the interface.
✗ Incorrect
The method Speak matches the interface method and implements Speaker.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to assign a type that does not implement Speaker.
✗ Incorrect
Only a value of type Person that implements Speaker can be assigned to s.
4fill in blank
hardFill 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'
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.
✗ Incorrect
The function parameter type should be Speaker and call the Speak method.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The map holds values of interface type Speaker. The value is a Person struct. The method called is Speak.