Go Interfaces Explained Like You're Interviewing for FAANG

Golang Interfaces
Golang Interfaces

Interfaces are basically contracts that every object which wants to get the benefits associated with those contracts must sign before gaining access to them. They are like the entrance exams to tech’s elite playground. Think of them as contracts that objects must sign before they can access certain benefits.

Let me explain.

You know how most big tech companies your Googles, Apples, and those guys at Facebook now pretending to be Meta require you to pass five different stages of interviews? Yep, five. Just to get that badge of honor, endless snacks, and health insurance that probably covers stuff like emotional trauma from debugging race conditions.

Now, once you pass those five stages, congratulations you’re officially in. People start calling you a FAANG developer, and everyone assumes you know what you’re doing (no pressure). The process is very explicit: you apply, you interview, you pass, and you’re in.

Now let’s flip the script.

In Go, interfaces work similarly but with a twist. Here’s the catch: interfaces in Go are implicit. That means you can be considered part of an interface without even knowing it. It’s like accidentally passing one round of interview at each FAANG company, and then out of nowhere, Facebook sends you an offer letter.

Surprise! You’re hired.

That’s the beauty of Go’s interface system. If your type happens to implement the methods required by an interface, congrats you just are that interface. No red tape, no explicit implementation. Let’s see how this looks in code.

The Code: Where It All Comes Together

package main

import "fmt"

type Shape interface {
	Area() int
	Perimeter() int
}

// Rectangle has signed the contract to be considered a SHAPE.
// It does this by implementing Area() and Perimeter() methods.
type Rectangle struct {
	width  int
	length int
}

func (r *Rectangle) Area() int {
	return r.width * r.length
}

func (r *Rectangle) Perimeter() int {
	return 2 * (r.length + r.width)
}

// Square has also signed the SHAPE contract by implementing the same methods.
type Square struct {
	length int
}

func (s *Square) Area() int {
	return s.length * s.length
}

func (s *Square) Perimeter() int {
	return 4 * s.length
}

func main() {
	rect := &Rectangle{width: 3, length: 6}
	square := &Square{length: 4}

	fmt.Println("===Rectangle Values=====")
	printArea(rect, "rectangle")
	printPerimeter(rect, "rectangle")

	fmt.Println("\n===Square Values=====")
	printArea(square, "square")
	printPerimeter(square, "square")
}

func printArea(s Shape, shapeName string) {
	fmt.Printf("The Area for the %s is: %d\n", shapeName, s.Area())
}

func printPerimeter(s Shape, shapeName string) {
	fmt.Printf("The Perimeter for the %s is: %d\n", shapeName, s.Perimeter())
}

Breakdown

Let’s talk through what’s going on in the code above

  • Implicit Interface Satisfaction: Neither Rectangle nor Square explicitly states they implement Shape, but Go is smart. Because they both have Area() and Perimeter(), they automatically satisfy the interface.

  • Polymorphism in Action: Functions like printArea and printPerimeter accept any Shape, not caring whether it’s a Square, Rectangle, or some exotic polygon you came up with at 2 AM hoping to win a “nobel laureate” in maths for it(chill I know there is no nobel laureate for maths)

Final Thoughts

Go’s interface system is a minimalist’s dream. No ceremony. No keywords like implements. Just write methods, and Go figures out the rest. It’s like being drafted into the Avengers because you already have the skills, they didn’t need your resume.

Now go forth and implicitly implement greatness.

Got questions or want to rant about interfaces? Drop a comment here or send me virtual coffee here.