An interface defines a set of methods that a type must implement.
It describes behavior, not data. Any type that has the required methods automatically satisfies the interface.
Unlike other languages, Go does not use the keyword implements.
A type automatically implements an interface if it has the required methods.
You can use an interface as a variable type.
This allows different types to be treated the same if they share behavior.
Multiple types can implement the same interface with different behavior.
This is how Go achieves polymorphism.
Interfaces are often used as function parameters.
This allows functions to work with multiple types.
The empty interface interface can hold values of any type.
It is useful for generic programming but should be used carefully.
Type assertions allow you to extract the actual value from an interface.
This is useful when working with unknown types.
Interfaces are commonly used to write flexible and reusable code.
Interfaces are a key part of Go’s design philosophy.
Be careful with these:
Create a Shape interface with an Area() method. Then implement it using Circle and Rectangle.