Alternative Patterns for Method Overloading in Golang

Click for: original source

Chris Tomich shares in this post 3 patterns, 2 of which can be used, to deal with Golang’s lack of method overloading – a functional way, an object oriented way, and a “JavaScript” way. Author realises that method overloading can be pretty badly abused but it’s perfect for default values.

The functional way example uses Golangs ability to return multiple values from a function and the ability to immediately accept those multiple values as an argument list for another function.

The object oriented way - is vastly more readable. And The JavaScript way example is also included, but author points out that it should be never really used in production.

Chris concludes that you will probably end up using a hybrid of the functional method and the object oriented methods. Example code for all above mentioned approaches in included.

[Read More]

Tags golang programming