Skip to content

Function Literals and Closure in Go

Posted on:May 11, 2020 at 10:40 PM

This is the seventh entry of my weekly series Learning Go. Last week I discussed Function Declarations, Arguments, Parameters, and Anonymous Functions. This week I will be talking about Function Literals and Closure.

Function Literals (Function Expressions)

Function Literals can be assigned to a variable or called (invoked) directly. They may refer to the variables defined in a surrounding function, making them a closure (we will talk more about this later in the post)

So, what is the difference between a Function Declaration and a Function Literal?

A Function Declaration binds an identifier (the function name) to a function. You can call this function by using its identifier.

A Function Literal is a closure, meaning they can reference variables that have been defined in a surrounding function. These variables can be shared between the function literal and the surrounding function. These variables persist as long as they are accessible.

Let’s start with a basic example and work our way up in complexity.

package main

import (
	"fmt"
)

func main() {
	f := func() {
		fmt.Println("I am a function literal!")
	}

	f()
	// I am a function literal!
}

Let’s see an example when a function literal has a parameter:

package main

import (
	"fmt"
)

func main() {
	f := func(x int) {
		fmt.Println("my birth year is ", x)
	}

	f(1990)
	// my birth year is 1990
}

Next, let’s see how we can return a function from a Function Literal:

package main

import (
	"fmt"
)

func main() {
	f := bar()
	fmt.Println(f())
	// 2020
}

func bar() func() int {
	return func() int {
		return 2020
	}
}

bar:

main:

As you can see from a few of these examples - function literals can be very powerful and can be used very dynamically in your code. Remember a few things when you are thinking of using a function literal instead of a function declaration:

Closure

the way that an anonymous function references variables declared outside of the anonymous function itself

A bit of a brain bender, huh?

The concept of closure can seem very abstract, which makes understanding how they work and the problems they solve difficult as well.

I am confident that seeing closure in action is the best way to learn how they work:

package main

import (
	"fmt"
)

func main() {
	a := incrementor()
	fmt.Println(a())
	// 1
	fmt.Println(a())
	// 2
	b := incrementor()
	fmt.Println(b())
	// 1
}

func incrementor() func() int {
	var x int
	return func() int {
		x++
		return x
	}
}

incrementor:

main:

Notice when we assign incrementor() to the variable b it does not return 3, why is that?

Although a and b were assigned the same return value of incrementor, b has only been invoked once; therefore, it holds it’s own unique value of 1.

This is the power of closure, data isolation. Now, you can easily use common actions across multiple variables, and those variables can have their own, unique values.

In Summary

I hope you have enjoyed learning about Function Literals and Closure. With the power of closure, you are equipped with another powerful feature of the Go programming language that can make your code more modular, readable, and scalable. Next, I will discuss Recursion and how to apply those principles to your functions. Can’t wait, see you then!

Never miss a post, subscribe to my Substack!