Implementing interfaces
📖 Scenario: You are building a simple program to manage different types of notifications in a system. Each notification type should be able to send a message in its own way.
🎯 Goal: Create a Go program that defines an interface for sending notifications and implements this interface with two different notification types: Email and SMS. Then, use these implementations to send messages.
📋 What You'll Learn
Define an interface called
Notifier with a method Send(message string)Create a struct called
Email with a field address stringCreate a struct called
SMS with a field number stringImplement the
Send method for both Email and SMS to print a message showing where the notification is sentCreate variables of type
Email and SMS with example dataUse the
Notifier interface to call Send on both notification typesPrint the output messages
💡 Why This Matters
🌍 Real World
Interfaces are used in real-world Go programs to write code that works with different types in a uniform way, such as sending notifications through email, SMS, or other channels without changing the main logic.
💼 Career
Understanding interfaces is essential for Go developers because many Go libraries and frameworks use interfaces to allow flexible and testable code.
Progress0 / 4 steps