The power of single-method interfaces in Go

submited by
Style Pass
2023-03-18 20:30:07

The other day I was pondering on the prevalence of single-method interfaces (SMI) in Go, and what makes them so effective and useful. SMIs have proven to be a very successful software modeling tool for Go programmers, and you find them all over Go code-bases.

I tried to think about the fundamentals, which brought me to some of the earliest roots of our trade: functional programming and higher-order functions (HOF). I discussed some examples of applying higher-order functions in Go recently.

The previous post demonstrated a Go solution using higher-order functions for the tree search problem described earlier. I encourage you to review the earlier posts to get the most out of this one.

Let's now see how the same task can be accomplished using SMIs instead of HOFs; the full code is on GitHub. Starting with the types:

These are the equivalent SMIs to the GoalP, Successors and Combiner function types we've seen before; the names are slightly modified to be more suitable for interfaces and their methods.

Leave a Comment