Demystifying Go (Golang), Google’s Programming Language: Interfaces

Demystifying Go (Golang), Google’s Programming Language: Interfaces

Introduction

Interfaces in Go serve a similar purpose as in other object-oriented languages: they define contracts that guarantee specific functions are present in any structure implementing them. With interfaces, we can apply the Liskov Substitution Principle, allowing one type to be substituted by another that satisfies the same interface, keeping the system modular and functional. In other words, when we implement an interface in Go, we ensure that any replaceable component implementing this interface will keep the system cohesive and stable.

Interfaces in Go

Unlike languages like Java or C#, where interfaces are explicitly implemented, Go automatically infers that a struct implements an interface, as long as it has the required methods. In Go, if a struct has the methods matching those of an interface, Go assumes the implementation – no implements declaration is needed. This is Go’s way of being concise and elegant.

Article content

Since both structs in this example have function signatures matching the interface, the compiler infers the implementation automatically. Pretty neat, right? The code is cleaner and more direct, but here's a heads-up: in larger systems, mapping which structs implement which interfaces can get tricky. So, keep your code organized and well-documented.

Interfaces with Pointers

Now, imagine you need to set values in a struct when calling a method from the interface, like storing a connection string, for instance. In this case, the receiver must be a pointer, allowing the method to modify the struct directly. When using pointers as receivers, we pass the struct’s address (with &) to the interface. Check out the example below, where MongoDatabase stores connection status using pointers.

Article content

Interface Composition

Interface composition is a practical way to create new interfaces by "extending" existing ones. Just declare the interface you want to complement when creating the new one. This allows us to combine functionalities without duplicating code – quite similar to struct composition but applied to interfaces and methods. See the example below, where we have two interfaces for reading and writing, combined within a general database interface.

Article content

Interface Type

In Go, each variable has a specific, unchangeable type during its lifetime because Go is a strongly typed language. But Go doesn’t leave us hanging when flexibility is needed. To create a repository that stores any type of data, whether it’s a Client, Product, or User, we can use the interface{} type, which accepts any value – be it a Go-native type or a custom type. This universal interface works like object in Java or any in TypeScript, making it the most abstract type in Go. Here’s how it works.

Article content

Note: Starting with version 1.8, Go introduced an alias for interface{}, called any, probably inspired by TypeScript.

Article content

Conclusion

Interfaces in Go offer a minimalist yet powerful approach to building flexible, robust, and maintainable systems. This flexibility is enhanced by implicit inference, which reduces code verbosity but requires extra attention to organization to avoid confusion in complex systems. By using interfaces in Go, you gain the clarity and elegance of a language that values the essentials – and if you master Go’s interfaces, you’ll be well-prepared to build robust, scalable, and highly efficient applications.

Patrick Cunha

Lead Fullstack Engineer | Typescript Software Engineer | Nestjs | Nodejs | Reactjs | AWS

5mo

Amazing

Like
Reply

Interesting, thanks for sharing! 👏

Like
Reply
Leandro Veiga

Senior Software Engineer | Full Stack Developer | C# | .NET | .NET Core | React | Amazon Web Service (AWS)

6mo

Very informative

Like
Reply
Rafael Andrade

Senior Data Engineer | Azure | AWS | Databricks | Snowflake | Apache Spark | Apache Kafka | Airflow | dbt | Python | PySpark | Certified

6mo

Valuable insights! Thanks for sharing, Vagner Nascimento.

To view or add a comment, sign in

More articles by Vagner Nascimento

Insights from the community

Others also viewed

Explore topics