TagAnonymous functions

Anonymous Functions in Go

Sometimes you need a function to be bound to a variable. Other times it is beneficial pass a function as an argument to another function. In this post, we will be looking at anonymous functions in Go. These anonymous functions allow us to create closures, easily pass around functions as arguments, and pass functions as returns.  

Anonymous Functions in Go

Anonymous functions are called such because they have no name associated with them. This can be very useful for code that we only want to be executed in certain situations. For instance, we can pass an anonymous function as an argument for another function. We can also make a function return another function. Let’s look at some simple examples.

In these examples, we have one function which takes a function as an argument. Our function declaration explicitly states that it takes a specific type of function. In this case, we are taking in a function that takes in a string and returns a string. This type of function, the argument function, is commonly called a callback function. 

We also have a function that returns a function. This function takes in no arguments and it returns a function that takes in an integer and returns an integer. Our return function is commonly referred to as a closure. Both of these types of functions are examples of higher-order functions in Go.

First Class Functions and Closures

Go features first-class functions. This basically means that Go treats functions as it would any data structure. You can pass functions as arguments, return them from other functions, and you can store them in variables. There are also other data structures that can be used to store functions which we will get into later. 

Let us talk about closures for a second here. A closure as its name implies is a function that closes off another function. We can use closures to specifically set the initial “environment” and have that “environment” be stored for all instances of the function.court-closure

If you look at this example, our outer function gives our inner function access to its variables. This means that those variables are only accessed by the inner function and the outer function which makes it easier to control how they are changed. Our closure is not returning a function, but it creates a closed scope for these functions and it creates a consistent initial environment. 


In this post, we looked at anonymous functions in Go. We talked about how we can use anonymous functions to create closures. We also looked at first class functions, higher order functions, and callback functions in Go. In our next few posts, we will talk about scope and look at pointers, structs, and interfaces in Go. 

Self-Invoking and Anonymous Functions in JavaScript

Functions are used in many different areas of JavaScript. Functions in JavaScript are a powerful way of freezing code for further use. Thus far, we used different functions to define object constructors and object behavior. In this post, we are going to look at some of the other ways that we can take advantage of functions in JavaScript.

Anonymous Functions in JavaScript

anonymous functionIn JavaScript, a typical function declaration utilizes the function keyword followed by the name of the function. We name our functions when we want to invoke them multiple different times in a piece of code. Sometimes, we want to be able to pass a function directly to another function as an argument. We were actually doing something like this when we were defining our getter and setter functions in our last post. The get: and set: keywords are both function shortcuts.

Most of the functions we’ve been writing inside of objects are called anonymous functions. Anonymous functions are functions that are not defined with a name. Typically, these functions are used for one of three main reasons.

The first way they are used is for direct variable definitions. This style of anonymous function is the most like a normal function declaration in that it is bound to a variable name. It can be invoked by calling that variable as an execution (with parenthesis). This is the type of anonymous function you will most commonly see inside of objects and constructors as well.

These are not the most realistic examples but they work for our purposes. These shaky examples just simply show that you can substitute a return value for a variable declaration in most cases. The second example displays a concept called side-effecting which we will talk about at length in a future post (the console.log() is the side-effect).

Callback Functions

Another common way that anonymous functions are used in JavaScript is as arguments for other functions.

callback anonymous functionsThe setTimeout() function is a very good example for this type of use. The first parameter that we pass to it is a function we want to run. The second parameter is the amount of milliseconds that we want to pass before this function runs. In this example, we’ve specified 3 secs or 3000 milliseconds before this anonymous function runs. You will see this type of anonymous function used many times when working with web applications. Typically, we call these functions callback functions.

Before we get to the last most common method of using anonymous functions, we need to define self-invoking functions.

Self-Invoking Functions

A self-invoking function is actually an anonymous function itself. A self-invoking function automatically runs on its creation and can only be run once. Here is the general format for this type of function.

self-invoking functionsFunctions run immediately in JavaScript when you place a set of parathesis after their names. This is an important thing to remember because in JavaScript you only put semicolons after execution statements. You may have noticed that after a function declaration you don’t put a semicolon. This is because the code is being stored for later. With self-invoking functions, however, we are not storing any code for later use. We are running the function right then and there.

The main reason to use a self-invoking function is for data privacy. Self-invoking functions are primarily used to create what is called a closure. A closure is a method of creating private variables inside of a function block. Closures can be used to create variables that act like the block scoped variables created by let as well. Closures are our last main use case for anonymous functions.


In JavaScript, functions have access to global variables and to variables defined inside of them. This can be a problem, for example, in web development, the global variables belong to the window object. Any script running on the page can actually access and manipulate any of these global variables. Look at this classic example.

With these examples, we can’t increment the count variable past one because every time we call the functions, we reset the count variable to zero. If we use a closure, however, this problem is easily solved.

closure-introIn this example, we set the add variable to the return value of a self-invoking function. This means that the outer function only runs one time, which sets counter to zero. The inner function works like a normal anonymous function. After the outer function runs from the declaration, when we call add(), only the inner function runs. In this example, the scope of counter is only inside of the self-invoking function and the add function. This means that after the self-invoking function runs, the add function is the only function that can touch it.


In this post, we took a look at the primary use cases for anonymous functions, for variable definitions, for callback functions and for creating closures. We also looked at self-invoking functions and how JavaScript scope can sometimes work against us.

In our next post, we will start to look at classes in JavaScript. As always, feel free to leave a comment if you thought we left anything out or if you need more clarification.