Menu
Coddy logo textTech

What is an Interface?

Part of the Logic & Flow section of Coddy's GO journey — lesson 12 of 68.

An interface in Go is like a contract that defines what behaviors a type must have, without specifying how those behaviors are implemented. Think of it as a collection of method signatures that describes what something can do.

To understand interfaces better, imagine a power outlet in your home.

The outlet has a specific shape and provides electricity, but it doesn't care what device you plug into it. Whether it's a lamp, a phone charger, or a computer, any device that has the right plug shape can connect and receive power. The outlet defines the "interface" - the requirements for connection.

In Go, an interface works similarly. It defines a set of method signatures that a type must have to "plug into" that interface. Any type that has all the required methods automatically satisfies the interface, regardless of what else that type might do or how it implements those methods.

This concept is powerful because it allows you to write code that works with any type that can perform certain behaviors, making your programs more flexible and reusable. Just like how different devices can all use the same power outlet, different Go types can all satisfy the same interface as long as they have the required methods.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

Cheat sheet

An interface in Go defines a contract of method signatures that a type must implement. Any type that has all the required methods automatically satisfies the interface.

Interfaces enable writing flexible code that works with any type that can perform certain behaviors, similar to how different devices can use the same power outlet as long as they have the right plug shape.

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow