Golang interface is one of things that people love due its simplicity and how they make possible to create small things which can be composed to create more complex ones.
It’s well know that defining interfaces in the implementations make them more flexible and reusable; defining interfaces with just one or a couple of functions signatures is one of the good practices of writing Golang applications, because they make the implementations more modular due they can be composed to get new ones for more specific needs.
Having this big set of small interface give the opportunity to have a bigger set of generic functions because they only constrain the parameters to implement one of those; so they can be use for any user defined type which implements at least that specific interface.
Everything above is well know and there are lots of posts about this matter, hence my goal in this post isn’t to add a new one more or extend this topic even more; there already good literature about it that you can find easily.
My goal is to provide the list of interfaces which I’ve found, and hopefully I haven’t missed any, in Golang 1.5 standard package and highlight few of them which I think that we have to bear in mind when we are developing common applications or services.
The sql package has two without including the ones defined in the driver subpackage which I’ve skipped due that in common applications development is unusual having to implement a new driver.
These ones gathered my attention due that it isn’t that rare to store types in a database which aren’t basic types defined in the used database system; while we can do the mapping each time that we read those values from the database, the Scanner interface is very convenient to be implemented and get the benefits to get the mapping automatically when the values are read from the database, by the Scan function.
One example of this case is go.uuid
The encoding/decoding data is another common operation to perform in common application development. Golang encoding package defines two pairs, marshare/unmarsheler, of interfaces, one for binary and another for text:
It also defines the same pair for specific encodings which usage is very spread
And one more for binary encoding * ByteOrder
Another common stuff is to pass argument to our application when it starts; flag package contains two interface which allow to define types to map values passed through command line arguments.
The fmt package defines a set of interfaces to format I/O, which is another functionality which can be needed in common applications development.
Some applications also requires to sort element in slices, so the types which implement sort interface can use after the routines of sort package to be sorted.
Golang interfaces are the most powerful and simple feature that the language expose to create modular and reusable implementations.
Golang standard library contains a bunch of already defined interfaces which can be implemented by our applications or services without having to think in new ones for the same purpose besides to ease and speed up to any Golang developer to understand our implementations.
Collecting all of them in in this post allowed me to discover some of them which I haven’t seen before even though I’ve been developing in Golang for one year and a half. Some of them I had never seen because I haven’t had to use them, others I had seen when I was reading a specific package documentation and other I saw at some point but I didn’t assimilate at the first glance, so this post has been a good exercise.
If I’ve missed any and you know which ones, please let me know with an innocent comment.
Thanks for reading it.