0
0
Goprogramming~10 mins

Interface use cases in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Interface use cases
Define Interface
Create Structs implementing Interface
Assign Structs to Interface Variable
Call Interface Methods
Use Polymorphism: Different Structs, Same Interface
Pass Interface to Functions
Interface Enables Flexible Code
This flow shows how interfaces are defined, implemented by structs, assigned, and used to enable flexible and polymorphic code.
Execution Sample
Go
package main
import "fmt"
type Speaker interface {
    Speak() string
}
type Dog struct {}
func (d Dog) Speak() string {
    return "Woof!"
}
func main() {
    var s Speaker
    d := Dog{}
    s = d
    fmt.Println(s.Speak())
}
Defines an interface Speaker and a struct Dog implementing Speak method.
Execution Table
StepActionEvaluationResult
1Define interface Speaker with Speak() stringInterface createdSpeaker interface ready
2Define struct DogStruct createdDog struct ready
3Implement Speak() method for DogMethod attachedDog implements Speaker
4Create Dog instance dd := Dog{}d is Dog{}
5Assign d to variable s of type Speakervar s Speaker = ds holds Dog as Speaker
6Call s.Speak()s.Speak()"Woof!" returned
7Pass s to function that accepts Speakerfunc greet(sp Speaker) { fmt.Println(sp.Speak()) }Function prints "Woof!"
8Create another struct Cat implementing SpeakerCat struct with Speak() returns "Meow!"Cat implements Speaker
9Assign Cat instance to s and call Speak()s = Cat{}s.Speak() returns "Meow!"
10ExitNo more stepsProgram ends
💡 All interface use cases demonstrated, program ends
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 9Final
dundefinedDog{}Dog{}Dog{}Dog{}
snilnilDog as SpeakerCat as SpeakerCat as Speaker
Key Moments - 3 Insights
Why can variable s hold different struct types?
Because s is of interface type Speaker, it can hold any value whose type implements the Speaker interface, as shown in execution_table rows 5 and 9.
What happens when we call s.Speak()?
The method of the actual struct stored in s is called, demonstrating polymorphism (rows 6 and 9).
Why define functions accepting interface types?
Functions accepting interfaces can work with any struct implementing that interface, making code flexible and reusable (row 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of s after step 5?
ACat struct stored as Speaker interface
Bnil
CDog struct stored as Speaker interface
DUndefined
💡 Hint
Check the 'Result' column in row 5 of execution_table
At which step does s hold a Cat struct?
AStep 6
BStep 9
CStep 7
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table row 9
If Dog did not implement Speak(), what would happen when assigning d to s at step 5?
ACompilation error
Bs would hold nil
Cs would hold d anyway
DRuntime panic
💡 Hint
Interfaces require implementing methods; see step 3 and 5 in execution_table
Concept Snapshot
Interface use cases in Go:
- Define interface with method signatures
- Structs implement interface by defining methods
- Interface variables can hold any implementing struct
- Enables polymorphism: same interface, different behaviors
- Pass interfaces to functions for flexible code
Full Transcript
This visual execution traces how interfaces work in Go. First, an interface Speaker is defined with a Speak method. Then, a struct Dog implements this method. A Dog instance is assigned to a variable s of type Speaker. Calling s.Speak() runs Dog's method, returning "Woof!". Later, another struct Cat also implements Speaker. Assigning Cat to s and calling Speak returns "Meow!". This shows how interfaces enable polymorphism and flexible code by allowing different structs to be used interchangeably through a common interface.