Go Functions (Part 4) — Anonymous Functions

Photo by tofayel ahmed on Unsplash

Anonymous functions are functions without a name. They are also known as Function literals.

You define anonymous functions using the usual func keyword, but with a slightly different syntax. first as a reminder, here’s what a regular function’s syntax looks like:

func FunctionName() {  

Now here’s what an Anonymous Function’s syntax looks like:

func() {  

Notice we have an extra pair of round brackets at the end. Those are used for passing in arguments; we’ll cover that a bit later. But for now, here’s a basic example:


Line 10: The anonymous function gets executed as Go works its way down the parent function’s code block. Also notice that this anonymous function is defined inside another function, more about this a bit later.

Let’s now take a look at what those round brackets are used for. The first pair of round brackets are used for setting up input parameters:


We can then pass arguments into the function using the second pair of round brackets.


This anonymous function only executes once during the program’s run. However you can make anonymous functions re-usable by saving it to a variable, then you can use that variable to call that function repeatedly. Here’s an example where we saved an anonymous function to a variable called “greetings” (line 10).


Line 12: We have to omit the trailing round brackets when saving a function literal to a variable. Those brackets are still being used but elsewhere (lines 16 and 17)

Line 14: This shows that our variable contains a function with the function signature of - one string input parameter and zero output parameters.

Setting up Output Parameters for Anonymous Functions

Anonymous functions can have output parameters. These output parameters are setup up and work in the same as in regular functions.


Closure Functions

Closure functions are Anonymous functions that use variables from its parent function. Here’s an example of this action.


Line 7: We created an integer variable, n, with an initial value of 5.

Lines 8–10: We defined an anonymous function and saved to a variable called counter

Line 11: We called the anonymous function using its variable name.

Line 13: This outputs 15. The interesting thing to note here is that the value 15 was set inside the anonymous function, but that change persisted to the parent level. In other words, Closer functions can access and change their parent’s variable.

Regular Functions Vs Anonymous Functions

In the above examples, we’ve seen that we can use variableName(argument) to call a function. This is syntactically similar to calling a regular function. So you might be wondering, how is a variable based anonymous function different to a regular function? There are a few differences:

  1. Anonymous functions can only be defined inside other functions.
  2. Anonymous functions can only be called from the body of its parent function.
  3. Anonymous functions can access variables from its parent function, i.e. Closure Functions.

So going back to the previous example, we can call our anonymous function from within the main function, e.g. line 11.

When should you use Anonymous Functions?

Once possible scenario could be that the function you’re writing keeps growing and growing until it turns into a “monster function”.

When that happens your first instinct might be to split your monster function into a less-intimadating monster function and several smaller regular functions. But that would mean that other code in your project would also be able to call those smaller functions, which you might not want.

So if you want to break down your monster function into smaller sub-functions, then you can create your sub-functions as Anonymous functions instead. That way, only your monster function will have sole access to those sub-functions.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store