Coming from Ruby, interfaces were an interesting concept because they reminded me of something we already do all the time: duck typing .
Go doesn't have inheritance like traditional OOP languages, but it has interfaces that allow different types to be used through the same behavior.
Let's see how that works.
What Is an Interface?
An interface defines a set of methods that a type must have to satisfy that interface.
It doesn't implement those methods itself. It only defines what methods are expected.
For example:
type Shape interface { Area ( ) float64 }
Enter fullscreen mode
Exit fullscreen mode
Here, we define an interface called Shape that requires any type satisfying it to have an Area() method returning a float64 .
Notice that we don't define how Area() calculates the area.…