Interface Pollution
In this article, we’ll look at a very common mistake that Golang new comers make, especially when they come from a background of Object Oriented Programming (OOP) languages like Java or C#. What is interface pollution? Let’s look at some codes. package employee type Employee struct { Name string Credit int } type EmployeeRepository interface { Query(name string) *Employee } func NewEmployeeRepository() EmployeeRepository { return &database{} } type database struct {} // Query returns the first Employee that has a matching name. func (d *database) Query(name string) *Employee { // ... } We implement an employee package, providing a method to query the storage by employee name, which is exposed through the API of the package: the EmployeeRepository interface. This interface construct abstracts database, the concrete implementation that does the real work. This simply follows good-old OOP design principles that we’re all used to, so what is the problem here? The first thing to note is that the EmployeeRepository interface has only one implementation. In fact, if we remove EmployeeRepository interface, and expose database to the outer world like the following code, the package API does not change. ...