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.

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.

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.

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.

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

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

4 个月

Amazing

回复
?ngelo Gabriel Albuquerque

Data Analyst | Data Engineer | GCP | AWS | Python | SQL

4 个月

Interesting, thanks for sharing! ??

回复
Leandro Veiga

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

4 个月

Very informative

回复
Rafael Andrade

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

4 个月

Valuable insights! Thanks for sharing, Vagner Nascimento.

要查看或添加评论,请登录

Vagner Nascimento的更多文章

社区洞察

其他会员也浏览了