In the documentation for the package found the following code:

check:=func(err error) { if err != nill { log.Fatal(err) } } 

Next, the code calls:

 check(err) 

How is it properly called and why do they do that? Why it was impossible to simply create an ordinary function:

 func check(err error) { if err != nil { log.Fatal(err) } } 

and call just like the previous one

  • I don't know golang , but maybe you guessed it, and is it a lambda-функция ? Although, as I looked at examples of functions in this language, everywhere this approach is used when decorating. But maybe these are just examples like this - kitscribe

2 answers 2

This is not an anonymous function or closure . This is a function as a variable (function as value).

Why it was impossible to simply create an ordinary function?

Without context is not clear. Perhaps the check variable will be redefined at the time of execution.

Anonymous function

Anonymous function - a function that cannot be redefined or recalled. In simple words, a function does not have a name (anonymous), therefore it is impossible to "call" or repeat it.

eg:

I

 callItAsCallback(data, func(response string) { print(response) }) 

https://play.golang.org/p/Z8InpVJL9W

II

 go func() { print("Hello!") } () 

https://play.golang.org/p/6Q0ITEMbta

Function as value

A function as a value is a function that can be redefined or recalled. The function has the name of the variable in which it is defined.

 var check func() if time.Now().Second()%2 == 0 { check = func() { print("четное") } } else { check = func() { print("нечетное") } } check() 

Short circuit

A closure is a function that stores data from the scope of a parent function. It is like the “character” of a function, its feature.

Closures are not possible without "higher order" functions (functions that return other functions). The exception gorutiny.

I In this example, the closure is used to encapsulate private values. Not very useful to go. https://play.golang.org/p/7_ES6AKjlI

II A classic example of a primitive "factory" (design pattern).

 package main func main() { MakePizza("сыром!")() MakePizza("грибами!")() } func MakePizza(with string) func() { msg := "Пицца с " return func() { println(msg + with) } } 

Pizza with cheese!

Pizza with mushrooms!

https://play.golang.org/p/RuQV2b04nn

The same factory, but without short circuit. This is a higher order function that returns an anonymous function . https://play.golang.org/p/YhAM0kYWba

III And finally, a classic example of the closure of gorutin in a cycle.

!!!! This example is obviously wrong!

 package main import "time" func main() { for i := 0; i < 10; i++ { go func() { println(i) }() } time.Sleep(time.Second) } 

10 10 10 10 10 10 10 10 10

https://play.golang.org/p/jfHE9mGCV4

The correct option , but this is the usual anonymous function .

 package main import "time" func main() { for i := 0; i < 10; i++ { go func(i int) { println(i) }(i) } time.Sleep(time.Second) } 

0 1 2 3 4 5 6 7 8 9

https://play.golang.org/p/lPFt-_HRY6

Conclusion

Anonymous functions are used as a callback (passed as a parameter to another function) or as a return value in "higher order" functions.

A function as a value can be used as a callback, as a return value in "higher order" functions, and for dynamically redefining the logic inside another function (unnecessarily, but possible).

A higher order function is a function returning other functions, both with and without closure.

A closure is an effect that can be carried by an anonymous or "function as a value" function returned from a higher order function or triggered as a horutin. The peculiarity is that the function can refer to the values ​​from the parent scope after the "end" of the parent function.

  • Hm And what is the example of the TC is not an anonymous function assigned to a variable? - VladD

It all depends on how often the function is called, perhaps the author tried to optimize the memory a little. If this is a huge package, then maybe it makes sense. Look at the package "encoding / json" there in the process of serialization is the compilation of a cache of methods for each data type, which further speeds up the encoding / decoding process.