0
0
Goprogramming~10 mins

Why interfaces are used in Go - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why interfaces are used
Define Interface
Create Types that Implement Interface
Use Interface Type to Hold Any Implementing Type
Call Methods via Interface
Achieve Flexibility and Abstraction
Interfaces define behavior without specifying exact types, allowing different types to be used interchangeably through shared methods.
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())
}
Defines an interface Speaker and a type Dog that implements Speak method.
Execution Table
StepActionEvaluationResult
1Define interface Speaker with Speak() methodSpeaker interface createdInterface expects Speak() string
2Define Dog structDog type createdDog has no methods yet
3Implement Speak() method for DogDog now implements SpeakerDog can be used as Speaker
4Create variable var s Speaker = Dog{}Dog instance assigned to Speaker variables holds Dog as Speaker
5Call s.Speak()Calls Dog's Speak methodReturns "Woof!"
6End of programNo more actionsProgram ends
💡 Program ends after calling Speak method on interface variable
Variable Tracker
VariableStartAfter Step 4After Step 5Final
snilDog instance assigneds.Speak() returns "Woof!"Dog instance remains
Key Moments - 3 Insights
Why can we assign a Dog to a variable of type Speaker without explicit conversion?
Because Dog implements the Speak() method required by Speaker, Go allows implicit assignment as shown in step 4 of the execution_table.
What happens when we call s.Speak() if s holds a Dog?
The Dog's Speak() method is called, returning "Woof!" as shown in step 5 of the execution_table.
Why use interfaces instead of concrete types directly?
Interfaces allow writing flexible code that works with any type implementing the interface, enabling abstraction and easier code reuse.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what type does variable s hold after step 4?
ASpeaker interface only
Bnil
CDog instance
Dstring
💡 Hint
Check the 'Action' and 'Result' columns in step 4 of execution_table
At which step does Dog become recognized as implementing Speaker?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Evaluation' column in step 3 of execution_table
If Dog did not implement Speak(), what would happen at step 4?
ACompilation error
Bs would hold nil
CDog assigned anyway
DRuntime panic
💡 Hint
Interfaces require implementing methods; see step 4 assignment in execution_table
Concept Snapshot
Interfaces define method sets without implementation.
Types implement interfaces by defining those methods.
Variables of interface type can hold any implementing type.
This enables flexible, abstract code that works with many types.
Go checks implementation implicitly at compile time.
Full Transcript
This visual execution shows why interfaces are used in Go. First, we define an interface Speaker with a Speak() method. Then, we create a Dog type and implement Speak() for it. Because Dog has the required method, it automatically implements Speaker. We assign a Dog instance to a variable of type Speaker. When we call Speak() on that variable, the Dog's method runs, returning "Woof!". This shows how interfaces allow different types to be used interchangeably, enabling flexible and abstract programming.