Menu

Learning Go as a Ruby Developer #8: Go Interfaces Are Basically Formal Duck Typing
📰

Learning Go as a Ruby Developer #8: Go Interfaces Are Basically Formal Duck Typing

DEV Community: ruby·Shrouk Abozeid·10 days ago
#Ne255f4D
#devtoruby#devto#dev#interface#type
Long article (956 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.