Recap - Shapes and Behaviors
Part of the Logic & Flow section of Coddy's GO journey — lesson 19 of 68.
Challenge
EasyIn this challenge, you'll create a communication system that demonstrates polymorphism through interfaces. You'll build a Speaker interface and implement it with different types that can "speak" in their own unique ways.
You will receive three inputs:
- A string representing the number of speakers to create (e.g.,
"3") - A string containing speaker types separated by commas (e.g.,
"person,parrot,person") - A string containing speaker names separated by commas (e.g.,
"Alice,Polly,Bob")
Your task is to:
- Define a
Speakerinterface with one method signature:Speak()that returns a string - Define a
Personstruct with aNamefield (string) - Define a
Parrotstruct with aNamefield (string) - Implement the
Speak()method for both structs:- Person should return:
"Hello, I'm [name]" - Parrot should return:
"Squawk! [name] says hello!"
- Person should return:
- Create a function called
makeAllSpeakthat takes a slice ofSpeakerinterfaces as a parameter - Inside
makeAllSpeak, iterate through the slice and call theSpeak()method on each speaker, printing the result - Parse the inputs to create the appropriate speaker instances based on the types and names provided
- Store all speakers in a slice of
Speakerinterfaces - Call
makeAllSpeakwith your slice of speakers
The speakers should be created in the order they appear in the input. For example, if the types are "person,parrot,person" and names are "Alice,Polly,Bob", create a Person named Alice, then a Parrot named Polly, then a Person named Bob. This challenge demonstrates how different types can be treated uniformly through a shared interface, showcasing the power of polymorphism in Go.
Try it yourself
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
// Read input
var numSpeakersStr string
var speakerTypesStr string
var speakerNamesStr string
fmt.Scanln(&numSpeakersStr)
fmt.Scanln(&speakerTypesStr)
fmt.Scanln(&speakerNamesStr)
// Parse input
numSpeakers, _ := strconv.Atoi(numSpeakersStr)
speakerTypes := strings.Split(speakerTypesStr, ",")
speakerNames := strings.Split(speakerNamesStr, ",")
// TODO: Write your code below
// 1. Define the Speaker interface
// 2. Define Person and Parrot structs
// 3. Implement Speak() methods for both structs
// 4. Create makeAllSpeak function
// 5. Create speakers based on input and store in a slice
// 6. Call makeAllSpeak with your slice
}All lessons in Logic & Flow
1Advanced Control Flow
Switch with `fallthrough`Breaking from Nested LoopsContinuing a Specific LoopThe `goto` StatementRecap - Advanced Loop Control4Project: Simple Task List
Project SetupAdding a Task2Structs and Methods
Defining Methods on StructsValue ReceiversPointer ReceiversChoosing ReceiversMethods vs FunctionsRecap - Struct Behavior5Maps In-Depth
Maps of StructsPointers as Map ValuesTesting for Nil MapsComparing MapsRecap - Word Frequency Counter3Interfaces (The Basics)
What is an Interface?Defining an InterfaceImplementing an InterfaceUsing Interface TypesEmpty InterfaceType AssertionsType SwitchRecap - Shapes and Behaviors