Documentation
¶
Overview ¶
anonymous demonstrates anonymous functions in Go
- Named functions can be written in Go at the package level only
- In addition, Go supports function literals that can be written even inside another function.
- A function literal is written like a normal function declaration, but without function name.
- It is considered as Go expression and its value is called anonymous function.
Why anonymous functions?
- Allows to define function at its point of use
- More importantly, those functions have access to their entire lexical environment
- So, a function value is not just action, but it can also have states.
Closures:
- In programming languages, a closure means binding a function with its environment.
- For example a function referencing a variable outside its body.
- Since Go has closures, anonymous functions can still has references outside variables and the scope of the variable is retained.
Click to show internal directories.
Click to hide internal directories.