DEV Community

Cover image for Learning Go as a Ruby Developer #8: Go Interfaces Are Basically Formal Duck Typing
Shrouk Abozeid
Shrouk Abozeid

Posted on AI-assisted

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 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. That's the responsibility of the concrete type.

Implementing an Interface

Let's say we have two types:

type Square struct {
    Side float64
}

type Circle struct {
    Radius float64
}
Enter fullscreen mode Exit fullscreen mode

Now we can implement the Area() method for both types:

func (s Square) Area() float64 {
    return s.Side * s.Side
}

func (c Circle) Area() float64 {
    return math.Pi * c.Radius * c.Radius
}
Enter fullscreen mode Exit fullscreen mode

Both Square and Circle now satisfy the Shape interface.

And here's something interesting about Go:

We don't explicitly declare that a type implements an interface.

There is no implements Shape keyword or declaration.

If a type has all the methods required by an interface, it satisfies that interface automatically.

Using an Interface

Now that both types satisfy Shape, we can use them through the interface:

func printArea(shape Shape) {
    fmt.Println(shape.Area())
}
Enter fullscreen mode Exit fullscreen mode

We can pass either a Square or a Circle to this function:

square := Square{Side: 5}
circle := Circle{Radius: 3}

printArea(square)
printArea(circle)
Enter fullscreen mode Exit fullscreen mode

The function doesn't need to know whether it received a Square or a Circle.

It only cares that the value can perform the behavior required by Shape.

This is one of the main benefits of interfaces: we can write code that depends on behavior rather than a specific concrete type.

Interface Variables

We can also declare a variable using an interface type:

var shape Shape

shape = Square{Side: 5}
fmt.Println(shape.Area())

shape = Circle{Radius: 3}
fmt.Println(shape.Area())
Enter fullscreen mode Exit fullscreen mode

The variable shape can hold different concrete values as long as those values satisfy the interface.

So we can change the concrete type assigned to it without changing the code that uses the interface.

A More Complete Example from My Notes

In my original notes, I used an interface called Shaper with two methods:

type Shaper interface {
    Area() float32
    Volume() float32
}
Enter fullscreen mode Exit fullscreen mode

Any type assigned to a Shaper variable must implement both methods.

For example:

type Square struct {
    Side float32
}

func (s Square) Area() float32 {
    return s.Side * s.Side
}

func (s Square) Volume() float32 {
    return 0
}
Enter fullscreen mode Exit fullscreen mode

Now Square satisfies Shaper.

If we only implement Area() and forget Volume(), the type won't satisfy the interface.

That's because an interface is not satisfied partially. It requires the complete set of methods defined in its declaration.

What About Ruby?

This is where things started to feel familiar.

In Ruby, we don't need to define an interface before using different objects in the same way.

We can simply rely on duck typing:

class Square
  attr_reader :side

  def initialize(side)
    @side = side
  end

  def area
    side * side
  end
end

class Circle
  attr_reader :radius

  def initialize(radius)
    @radius = radius
  end

  def area
    Math::PI * radius * radius
  end
end

def print_area(shape)
  puts shape.area
end
Enter fullscreen mode Exit fullscreen mode

We can pass both objects to print_area:

print_area(Square.new(5))
print_area(Circle.new(3))
Enter fullscreen mode Exit fullscreen mode

Ruby doesn't care about the object's class. It cares that the object responds to area when the method is called.

That's duck typing:

If it walks like a duck and quacks like a duck, it's a duck.

Go follows a similar idea, but makes the required behavior explicit through an interface.

Go vs Ruby

Go Ruby
Defining required behavior Interface Usually implicit
How a type satisfies it Implements all required methods Responds to the methods being called
Explicit declaration No, satisfaction is implicit No interface declaration required
Type checking Compile time Mostly runtime
Polymorphism Interfaces Duck typing

Main Idea

Go interfaces reminded me a lot of Ruby's duck typing, but with a more explicit contract.

In Ruby, I can pass any object to a method as long as it responds to the methods I call.

In Go, I define that contract using an interface, and the compiler checks whether the type satisfies it.

I think this is one of the concepts that makes Go interesting coming from Ruby. It gives us a way to write flexible code without relying on inheritance, while still keeping things explicit and checked at compile time.

Top comments (0)