Before explaining the closure, take a look at the anonymous functions in Golang.
Anonymous Functions
Anonymous functions can also be called function literals, lambda functions, or closures. The concept of closure originates from the mathematical evaluation of expressions in lambda calculus. Technically, there is a subtle difference between anonymous functions and closures: anonymous functions are functions without names, while closures are instances of functions. To implement closures in Golang, anonymous functions are indispensable.
Let's first look at an example of a normal function, for example:
func add(x, y int) { (x + y) }
The call method is as follows:
add(1, 2) // Output 3
Next, let’s see how to use anonymous functions to achieve the same function:
func(x, y int) { (x + y) }(1, 2)
The function of this anonymous function is the same as the ordinary function above. The difference is
- No name
- Call it directly after definition
Next, use an anonymous function by creating a function that returns a function. Functions generally return basic types such as integers, strings, structures, etc., but in Golang, one function can return another function. Here is an official example from Golang:
func adder() func(int) int { sum := 0 return func(x int) int { sum += x return sum } }
The return type of this function is a function of type func(int) int. You can assign the return value of this function to a variable, and then you can call this variable like a function, for example:
pos := adder() pos(1)
Closures
Through the above explanation, we have already known the definition and usage of anonymous functions, and we also understand that one function can return another function. Next, we will explain the closure.
In Golang, a closure is a function that references variables outside the scope. The closure can exist for longer than the scope where it was created, so it can access variables in that scope, even after the scope is destroyed. The adder() function above returns a typical closure.
Anonymous functions in Golang are also called closures. Anonymous functions are special types of functions. Without names, closures can be considered as special types of anonymous functions.
The closure in Golang consists of two parts: the function body and the context environment when the function is executed. The function body defines the logic of the closure, and the context environment contains variables outside the function. When a closure is created, references to external variables are saved in the context, and these external variables can be accessed at any time inside the function body. Let’s take a look at an example of slightly modifying the adder() function above:
package main import "fmt" func adder() func(int) int { sum := 0 return func(x int) int { ("Before execution sum =", sum) sum += x return sum } } func main() { pos := adder() for i := 0; i < 4; i++ { ("After execution sum =", pos(1)) } }
The operation results are as follows:
Before execution sum = 0
After execution sum = 1
Before execution sum = 1
After execution sum = 2
Before execution sum = 2
After execution sum = 3
Before execution sum = 3
After execution sum = 4
It can be seen that the external variables referenced by the closure function are saved in the context environment (it has not been destroyed). Every time the closure is executed, the variables in the closure save the value after the last run.
summary
Closures are a feature of functional programming languages. Functions can either return a function or accept a function as a parameter (this function is called a higher-order function). Golang also supports functional programming, closures are very widely used in Golang and are often used with Goroutine and channel.
This is the end of this article about detailed explanation of closures in Golang. For more related content on Golang, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!