Richard Bucker

Compose with functions, not methods

Posted at — Feb 11, 2015

I was drawn to a link titled “Compose with functions, not methods”. It was a talk that Rob Pike (a Go Author) gave in 2012. While I had an idea what he was talking about I looked at the slide and read and read it trying to gleam what he meant by the text.I then read the method and function definitions in the Golang documentation called “Effective Go”. And I still did not understand what the authors were getting to. Then I read the language reference and found the missing pieces.Function: “A function declaration binds an identifier, the function name, to a function”Method: “A method is a function with a receiver. A method declaration binds an identifier, the method name, to a method, and associates the method with the receiver’s base type."And now it all made sense. By preferring functions instead of methods one can use interfaces as part of the composition formula in the way that Go implements Read() across different packages. And the same for wrappers where the work can be nested.