Interface definition
📖 Scenario: You are building a simple program to represent different types of vehicles. Each vehicle can describe itself by returning its type as a string.
🎯 Goal: Create an interface called Vehicle with a method Type() that returns a string. Then create two structs Car and Bike that implement this interface.
📋 What You'll Learn
Create an interface named
Vehicle with a method Type() stringCreate a struct named
CarCreate a struct named
BikeImplement the
Type() method for both Car and Bike to return their type as a stringCreate variables of type
Car and BikeUse the
Vehicle interface to call the Type() method on both variablesPrint the results
💡 Why This Matters
🌍 Real World
Interfaces let you write code that works with different types in a uniform way, like handling different vehicles in a transport app.
💼 Career
Understanding interfaces is key for Go developers to build modular, testable, and maintainable software.
Progress0 / 4 steps