0
0
Goprogramming~10 mins

Interface satisfaction 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'
AListen
BSpeak
CTalk
DRun
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name not related to speaking, like 'Run'.
Forgetting the parentheses after the method name.
2fill in blank
medium

Complete the code to make type Dog satisfy the Speaker interface by implementing Speak method.

Go
package main

import "fmt"

type Dog struct {}

func (d Dog) [1]() {
	fmt.Println("Woof!")
}
Drag options to blanks, or click blank then click option'
ASpeak
BTalk
CRun
DListen
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name like 'Talk' or 'Run'.
Missing the method receiver in the function.
3fill in blank
hard

Fix the error in the code to make type Cat satisfy the Speaker interface.

Go
package main

import "fmt"

type Cat struct {}

func (c Cat) [1]() {
	fmt.Println("Meow!")
}
Drag options to blanks, or click blank then click option'
ASpeak
BListen
CRun
DTalk
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name.
Changing the method signature (parameters or return type).
4fill in blank
hard

Fill both blanks to declare an interface and a type that satisfies it.

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

type Bird struct {}

func (b Bird) [2]() {
	fmt.Println("Tweet!")
}
Drag options to blanks, or click blank then click option'
ASinger
BSpeak
CTalk
DRunner
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in interface and type.
Naming interface with a verb instead of a noun.
5fill in blank
hard

Fill all three blanks to create a function that accepts any Speaker and calls its Speak method.

Go
func [1](s [2]) {
	s.[3]()
}
Drag options to blanks, or click blank then click option'
AMakeSound
BSpeaker
CSpeak
DAnimal
Attempts:
3 left
💡 Hint
Common Mistakes
Using a concrete type instead of the interface as parameter.
Calling a method name not defined in the interface.