Let’s dissect interfaces and be come masters of the trade
I feel obliged to make an article about interfaces since it’s one of the features in Go that amazed me the most. I’m not gonna lie, it took me a while to get comfortable using them, but when I passed the learning threshold I started loving them.
It’s not that Interfaces are hard or complex, but learning the habit to use them and the mindset seem to confuse many. In this article I will try to use a lot of examples so that we not only learn how to use them, but also when.
Let’s start at the beginning and review the basics before we move on.
An interface is a collection of method receivers. What that means is that we define a rule, that any type that has ALL methods described, is part of an interface. It might be worth reminding people what a method receiver is as well. It’s when we apply a method to a type. The gist below shows an example, this works for both pointers and non-pointers.
#programming #golang #go