Interface satisfaction
📖 Scenario: You are building a simple program to check if different types satisfy a common interface. This is useful when you want to ensure that different objects can be used interchangeably based on shared behavior.
🎯 Goal: Create a Go program that defines an interface and two structs. Then check if these structs satisfy the interface by assigning them to interface variables.
📋 What You'll Learn
Define an interface called
Speaker with a method Speak() stringCreate a struct called
Dog with a method Speak() string that returns "Woof!"Create a struct called
Cat with a method Speak() string that returns "Meow!"Assign instances of
Dog and Cat to variables of type SpeakerPrint the result of calling
Speak() on both interface variables💡 Why This Matters
🌍 Real World
Interfaces let you write flexible programs where different types can be used interchangeably if they share behavior. For example, different animals can be treated as speakers without knowing their exact type.
💼 Career
Understanding interface satisfaction is key for Go developers to design clean, modular, and testable code. It is widely used in real-world Go projects and libraries.
Progress0 / 4 steps