0
0
Goprogramming~10 mins

Interface definition in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Interface definition
Define interface with method signatures
Create struct type
Implement methods on struct matching interface
Assign struct instance to interface variable
Call interface methods
Use polymorphism
This flow shows how to define an interface, implement it with a struct, assign the struct to the interface, and call its methods.
Execution Sample
Go
package main
import "fmt"
type Speaker interface {
    Speak() string
}
type Person struct {
    Name string
}
func (p Person) Speak() string {
    return "Hello, " + p.Name
}
func main() {
    var s Speaker
    s = Person{Name: "Alice"}
    fmt.Println(s.Speak())
}
This code defines an interface Speaker with one method Speak, implements it with Person struct, assigns a Person to Speaker, and calls Speak.
Execution Table
StepActionEvaluationResult
1Define interface Speaker with method Speak()Speaker interface createdInterface ready to use
2Define struct Person with field NamePerson struct createdStruct ready to use
3Implement Speak() method on PersonMethod matches Speaker interfacePerson implements Speaker
4Declare variable s of type Speakers is nil interface variables ready to hold Speaker
5Assign Person{Name: "Alice"} to sPerson instance assigned to ss holds Person as Speaker
6Call s.Speak()Calls Person.Speak()Returns "Hello, Alice"
7Print outputOutput printed to consoleHello, Alice
8Program endsNo more codeExecution stops
💡 Program ends after printing the greeting from the interface method
Variable Tracker
VariableStartAfter Step 5After Step 6Final
snilPerson{Name: "Alice"}Person{Name: "Alice"}Person{Name: "Alice"}
Key Moments - 3 Insights
Why can we assign a Person struct to a Speaker interface variable?
Because Person has a method Speak() with the same signature as the Speaker interface requires, so Go treats Person as implementing Speaker (see execution_table step 5).
What happens when we call s.Speak() if s holds a Person?
The Speak() method of Person is called, returning the greeting string (see execution_table step 6).
Why is the interface variable s initially nil?
Because declaring var s Speaker creates an interface variable with no value until assigned (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of variable s after step 5?
APerson{Name: "Alice"}
Bnil
CSpeaker interface type
DEmpty struct
💡 Hint
Check variable_tracker column 'After Step 5' for s value
At which step does the program call the Speak() method?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
See execution_table row where Action is 'Call s.Speak()'
If Person did not implement Speak(), what would happen when assigning to s?
As would hold a nil value
BAssignment would fail with a compile error
CProgram would panic at runtime
DAssignment would succeed but method call fail
💡 Hint
Interfaces require matching methods; see key_moments about assignment
Concept Snapshot
Interface definition in Go:
- Use 'type Name interface { MethodSignatures }'
- Any type with matching methods implements the interface
- Assign instances to interface variables
- Call methods via interface for polymorphism
- No explicit 'implements' keyword needed
Full Transcript
This example shows how to define an interface named Speaker with one method Speak. Then we create a struct Person with a Name field. We implement the Speak method on Person to return a greeting string. In main, we declare a variable s of type Speaker, initially nil. We assign a Person instance with Name 'Alice' to s. Because Person implements Speak, this assignment works. Calling s.Speak() calls Person's method and returns 'Hello, Alice'. Finally, we print the result. The program ends after printing. This demonstrates Go's interface definition and usage with method implementation and polymorphism.