Golang - Interfaces - Interfaces Explained

Publish date: 2025-09-24
Tags: <a href="https://programmercave.com/tags/Go/">Go</a>

Introduction

An interface in Go is a type that defines a set of method signatures. It’s a contract that specifies what a type can do. If a type provides definitions for all the methods in an interface, it is said to satisfy that interface.

Analogy: Think of an interface as a job description.


Implicit Implementation: The Core of Go Interfaces

This is the most important concept to understand. In many other languages, a class must explicitly declare that it implements an interface (e.g., class MyWriter implements Writer).

In Go, the implementation is implicit. If a type has all the methods required by an interface, it automatically satisfies that interface. There is no implements keyword.

Why is this so powerful?

Example: A Shape Interface

Let’s define our own interface called Shape. A shape is anything that has an area.

// Shape is an interface for anything that has an area.
type Shape interface {
    Area() float64
}

Now, any type that has an Area() float64 method will automatically satisfy this interface.

package main

import (
	"fmt"
	"math"
)

// Shape is an interface for anything that has an area.
type Shape interface {
	Area() float64
}

// Circle is a struct representing a circle.
type Circle struct {
	Radius float64
}

// By implementing this method, Circle now satisfies the Shape interface.
func (c Circle) Area() float64 {
	return math.Pi * c.Radius * c.Radius
}

// Rectangle is a completely different type.
type Rectangle struct {
	Width  float64
	Height float64
}

// Rectangle also satisfies the Shape interface.
func (r Rectangle) Area() float64 {
	return r.Width * r.Height
}

func main() {
	myCircle := Circle{Radius: 5}
	myRectangle := Rectangle{Width: 10, Height: 4}

	// We can use a function that accepts any Shape.
	printArea(myCircle)
	printArea(myRectangle)
}

// This function works with ANY type that satisfies the Shape interface.
func printArea(s Shape) {
	fmt.Printf("The area of the shape is %0.2f\n", s.Area())
}

Polymorphism in Go

Interfaces are Go’s primary tool for polymorphism. You can write a single function that operates on an interface type, and that function can then accept values of any concrete type that satisfies the interface.

// This function can take any type that satisfies the Shape interface.
func printArea(s Shape) {
    fmt.Printf("The area of the shape is %0.2f\n", s.Area())
}

func main() {
    myCircle := Circle{Radius: 5}
    myRectangle := Rectangle{Width: 10, Height: 4}

    printArea(myCircle)    // Works with a Circle
    printArea(myRectangle) // Works with a Rectangle
}

Key Takeaways for Interviews

Tags: <a href="https://programmercave.com/tags/Go/">Go</a>