Michael Sauter is a German web developer at SitePoint. He's maintaining the backend of the various SitePoint sites and works on new features and products. Usually working with Ruby or PHP - currently interested in Go and Docker.
Ever heard of “The best interface is no interface”? While that may be true for product design, it certainly isn’t when it comes to programming Go. On the contrary, it’s probably better to follow the principle “err on the side of too many interfaces”. Why? In this article, we’ll look at what interfaces are, how they are implemented in Go and why they are so useful. Let’s dive right in!
What are interfaces?
Generally speaking, an interface defines the behavior of an object and therefore how this object can be used by other parts of the system. The interface describes what can be done by the object, but not how the object actually caries out the instructions. As a consequence, the implementation of the object can be exchanged without breaking functionality for the consumer.
In languages like PHP or Java, interfaces specify which methods a class must implement, and in turn each class declares which interfaces it implements. Many classes can implement the same interface, and the system can then operate on the interfaces instead of the implementation.
Interfaces in Go
In Go, interfaces have the same purpose, but there are a few differences to traditional languages which make interfaces much more useful and versatile. So, what are interfaces in Go? Firstly, an interface in Go is simply defined as a set of methods. For example, the Reader interface from the standard library looks like this:
This entry is part 4 of 4 in the series Redesigning SitePointOne of the objectives for developers of the new SitePoint website was to make it fast. Really fast. Before we dive into what we did, I want to show you where we’re coming from: Time to first byte: ~3 seconds Document complete: 6-9 seconds […]
This entry is part 3 of 4 in the series Redesigning SitePointIf you’ve ever set up WordPress, you know that it’s super easy and fast to install. However, if you’ve ever tried to set up WordPress on multiple servers, deploying automatically from a repository or running on different environments, you know that these kind of […]
Sure, most of the SitePoint Christmas Sale 2012 fun is in the front end, but we still need a back end serving the data. This is how we did it.