Function Currying in GoLang

Manik Khandelwal
1 min readJan 13, 2021

Currying is the process of converting a function which takes many arguments into a series of function that each takes just one argument.
For example, if we have a simple function named add which takes two arguments 1 and 2. We can break this function into a series of function which takes only one argument.

three := add(1, 2)
func add1 := + 1
three == add1(2)

Why might you want to write these types of functions?

To understand this let’s consider an example:

public String doSomething(a String, b String, c Obj3, d Obj4) {
return a + " " + b + c.ToString() + d.ToString()
}

This function takes a lot of parameters making it hard to compose the function. Hence in these types of languages, you don’t see function composition happening all that often. Instead, you end up with rather large methods that in the best case aren’t reusable but don’t violate the Single-Responsibility Principle, and in the worst-case encapsulate too much functionality and are a nightmare to maintain.

Our goal should be to have functions that do one thing and do one thing well. By removing the chain of arguments we can split up our ‘monolith function’ into smaller and composable parts.

--

--