Urgent.News

What's breaking now, across hundreds of outlets.

Tech

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

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…

As someone who has worked with Ruby, the concept of interfaces in Go was intriguing. In Ruby, we often employ what's known as "duck typing," where the type or class of an object is less important than the methods it can perform. Go's interfaces, however, require a concrete type to implement all the methods defined in the interface.

An interface in Go is essentially a contract that specifies a set of methods a type must implement to satisfy the interface. It doesn't implement those methods itself; it merely declares the methods that any type claiming to fulfill the interface must possess. For instance, we can define an interface called Shape with a method called Area() that returns a float64.

The concrete types implementing this interface, such as Square and Circle, are not obligated to define how the Area() method is calculated; that is the responsibility of the implementing type.

Interestingly, there's no need to explicitly declare that a type implements an interface in Go. If a type has all the methods required by an interface, it implicitly satisfies that interface. This contrasts with Ruby, where we define an interface upfront and ensure that classes implement it.

Once we have an interface, we can use it to write flexible, behavior-focused code. For example, we can create a function that accepts a Shape as an argument and call it with either a Square or a Circle without needing to know their concrete types. This decouples the function from knowing about the specific types, allowing us to write more generic and reusable code.

Interfaces can also be assigned to variables, which can hold different concrete types as long as those types satisfy the interface. This provides flexibility in changing the underlying type without altering the code that uses the interface.

In an example from my notes, an interface called Shaper was introduced with two methods: Area() and Volume(). A Square type was defined with an Area() method but lacked the Volume() method, thus not satisfying the Shaper interface. This shows that an interface must be fully implemented; partial implementations are not accepted.

Comparing this to Ruby, we see a similar scenario where we might simply rely on duck typing. We define classes like Square and Circle and implement an area method for each. Then, we can write a function that takes an object with an area method, regardless of its class, demonstrating Ruby's implicit reliance on the object's ability to fulfill the required behavior.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Metered Billing Evidence: Usage Snapshots Explained for Logistics Access Reviews

Metered billing design Where to store usage snapshots Evidence the reviewer must receive Shared database, separate permissions Store them beside accepted usage when one team can enforce the collection…

  • Usage snapshots stored alongside accepted usage with separate permissions
  • Define billable unit as one completed shipment-status report
  • Implement metered billing using shared database with separate permissions

More from Wednesday 25 November →