This is the 15th day of my participation in the August Text Challenge.More challenges in August

What is a closure

A closure is an entity composed of a function and its environment (closure = function + reference environment)

According to Wikipedia, a Closure is a function that refers to a free variable. A closure is implemented as a structure that stores a function (usually its entry address) and an associated environment (equivalent to a symbol lookup table). The referenced free variable will exist with the function, even if it has left the environment in which it was created. Therefore, there is another way to say that closures are entities composed of functions and their associated reference environments. Closures can have multiple instances at run time, and different reference environments and the same combination of functions can produce different instances.

Anonymous functions

An anonymous function is an implementation of a function that does not require a function name. An anonymous function consists of a function declaration and a function body without a function name.

It is possible to define another function within a function.

The advantage of anonymous functions is that you can use variables directly within the function without having to declare them

func getSequence() func() int {
   i:=0
   return func() int {
      i+=1
     return i  
   }
}
Copy the code

The func above is the anonymous function.

import (
"fmt"
"math"
   "testing"
)
func TestNiMing(t *testing.T) {
   getSqrt := func(a float64) float64 {
      return math.Sqrt(a)
   }
   fmt.Println(getSqrt(4))
}
Copy the code

Output result:

2
Copy the code

Closure code

package bibao import ( "fmt" "testing" ) func NextNumber() func() int { i := 0 b := func() int { i++ fmt.Println(i) Return I} return b} func TestBiBao(t * testing.t) {next := NextNumber() next() next() next() next() NextNumber() NextNumber()Copy the code

Output result:

=== RUN TestBiBao 1 2 3 -- PASS: TestBiBao (0.00s) PASSCopy the code

See, closures are a little bit like anonymous functions.

The Go language implements closures through anonymous functions.

The nature of closures

A closure is a code block that contains free variables that are not defined within the code block or in any global context, but in the context in which the code block is defined.

Since free variables are in a code block, as long as they are used more than a closure, the free variables and the referenced objects are not released in the computer environment where code free variable bindings are to be performed

That is, closures have memory effects. The logic in a closure can modify the variables captured by the closure, and the variables will persist throughout the life of the closure. The closure itself has memory effects just like the variables.

func NextNumber() func() int { i := 0 b := func() int { i++ fmt.Println(i) return i } return b } func TestBiBao(t *testing.T) {next := NextNumber() next() next() next() next() next1 := NextNumber() next1() NextNumber() NextNumber()Copy the code

Execution Result:

=== RUN TestBiBao 1 2 3 1 -- PASS: TestBiBao (0.00s) PASSCopy the code

You can open a new closure with a new object approach