anonymous

command
v0.0.0-...-a716749 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL