The Way of Interface in Go

David Lee
4 min readMay 2, 2023

In Go , interfaces are custom types that define a set of function signatures. By design , interfaces are considered to be tools that make code clean, short and provide a good API between packages, servers and clients. Go interfaces enable developers to treat other types as one type temporarily as long as these types have the functions defined in the interface. When a type has the functions defined in an interface, we say that that type implements that interface.

When using interfaces , you are not allowed to create an instance of an interface, but rather, you can create a variable of an interface type and this variable can be assigned to a value type that implements the interface.

Interfaces are an important feature in Go because they provide a way to define common behavior for disparate types. They allow functions to be written in a generic way, without knowledge of the exact types they will be working with. This enables polymorphism, which is the ability for objects of different types to be treated as if they were of the same type.

By defining an interface, a type can declare that it implements certain methods, which allows it to be used anywhere that the interface is expected. This allows for modular code design, where components can be built and tested independently, as long as they conform to a shared interface.

--

--

David Lee

Gopher and Rustacean -- > ⾶ 🤺 I use VIM btw 🤫