Package chainable provides an easy and convenient way of chaining function calls in Golang.
It's common to find Go programs similar to the one below:
Error handling can make the codebase messy. At the same time, chaining function calls can obfuscate
errors and cascading calls can increase the cognitive complexity.
Elixir, F# and other languages solve this problem with the support to pipes, but Golang hasn't such
Chainable provides a clearer way to chain function calls, using the output of the previous function
as the input of the next one. The example above could be re-written using Chainable as follow:
Another advantage is that Chainable automatically handle errors in a chain.
Thus, if one of the methods returns an error as the last argument, the chain is broken
and the error is returned by the "Unwrap" method:
If automatic error handling isn't desired (i.e. the error should be chained along with the other arguments),
the method "ChainDummy" should be used instead of "Chain". Pay attention that the next function in the chain
must be able to receive the error generated by the previous one:
"Chain" and "DummyChain" methods are variadics, and can be used in conjunction:
Finally, to reset a chain and make it ready to be reused, just call the method "Reset":