0
0
Goprogramming~10 mins

Implementing interfaces in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Implementing interfaces
Define Interface
Define Struct
Implement Interface Methods on Struct
Create Struct Instance
Assign to Interface Variable
Call Interface Methods
Use Polymorphism
This flow shows how a struct implements an interface by defining required methods, then using the struct through the interface type.
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 = Dog{}
  fmt.Println(s.Speak())
}
This code defines an interface Speaker and a struct Dog that implements it, then calls Speak through the interface.
Execution Table
StepActionEvaluationResult
1Define interface Speaker with method Speak()Interface createdSpeaker interface ready
2Define struct DogStruct createdDog struct ready
3Implement Speak() method on DogMethod attachedDog now implements Speaker
4Create Dog instance and assign to Speaker variable sDog{} assigned to ss holds Dog as Speaker
5Call s.Speak()Calls Dog.Speak()"Woof!" returned and printed
6Program endsNo more codeExecution stops
💡 Program ends after printing "Woof!"
Variable Tracker
VariableStartAfter Step 4After Step 5Final
snilDog{} as SpeakerDog{} as SpeakerDog{} as Speaker
Key Moments - 3 Insights
Why can we assign Dog{} to a variable of type Speaker without explicit casting?
Because Dog implements all methods of Speaker (Speak()), Go allows implicit assignment as shown in step 4 of the execution_table.
What happens if Dog does not implement Speak() method?
The assignment in step 4 would fail with a compile-time error, because Dog wouldn't satisfy the Speaker interface.
When calling s.Speak(), which method runs?
The Dog's Speak() method runs, as s holds a Dog instance implementing Speaker, shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of variable s after step 4?
ASpeaker interface without value
Bnil
CDog{} as Speaker
DDog struct without interface
💡 Hint
Check variable_tracker row for s after step 4
At which step does Dog become recognized as implementing Speaker?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table step 3 where Speak() method is attached
If Dog did not have Speak() method, what would happen at step 4?
ACompile-time error occurs
BProgram runs but panics at runtime
CAssignment succeeds with nil value
DDog is assigned but Speak() returns empty string
💡 Hint
Refer to key_moments about missing method causing assignment failure
Concept Snapshot
Go interfaces define method sets.
A struct implements an interface by having those methods.
No explicit declaration needed.
Assign struct instance to interface variable.
Call methods via interface for polymorphism.
Full Transcript
This example shows how in Go, interfaces are implemented by structs automatically when the struct has all the methods the interface requires. We define an interface Speaker with method Speak(). Then we define a struct Dog and add a Speak() method to it. Because Dog has Speak(), it implements Speaker. We create a Dog instance and assign it to a variable of type Speaker. When we call Speak() on that variable, Dog's Speak() runs and returns "Woof!". This demonstrates Go's interface implementation and polymorphism.