This is a creation in
Article, where the information may have evolved or changed.
The go language supports anonymous functions, where functions can be passed or used like normal variables.
Here's how to use it:
Main.go
Package Mainimport ("FMT") Func main () {varV Func (Aint)intv= Func (Aint)int { returnAA} fmt. Println (V (6))//two ways to do itV1: = Func (iint)int { returnIi} fmt. Println (v1 (7))}
The anonymous function of the go language is closures, and the following is the explanation of closures in go language programming
Basic concepts
Closures are blocks of code that can contain variables that are free (not bound to specific objects), that are not defined within the code block or
in any global context, but are defined in the context in which the code block is defined. The block of code to execute (because the free variable contains
in the code block, so these free variables and the objects they refer to are not freed) provide a binding calculation ring for the free variable
boundary (scope).
Closure Value
The value of a closure is that it can be used as a function object or as an anonymous function, which means, for a type system, not only that the
data also represents the code. Most languages that support closures use functions as first-level objects, meaning that they can be stored in
variables as arguments to other functions, and most importantly, can be dynamically created and returned by a function.