closure

command
v0.0.0-...-a2a1f02 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2020 License: MIT Imports: 1 Imported by: 0

README

closure example

An example of closure (a function that references variables from outside its body).

GitHub Webpage

CLOSURE

A closure is a function value that references variables from outside its body.

Helps us limit the scope of variables used by multiple functions. Without closure, for two or more functions to have access to the same variable, that variable would need to be at the package scope.

There are two methods as follows.

ASSIGN ANONYMOUS FUNCTION (func LITERAL) TO A VARIABLE

Here is an example of closure, assigning an anonymous function (also called func literal) to a variable increment1.

This is called closure because the function captures (enclose) the surrounding environment and can use it.

RETURN A FUNCTION TO A FUNCTION

Same program as above, but with function outside main.

But this acts like a variable where once increment is declared and assigned, x is set in that scope.

Just think/treat the function as a variable and closure makes sense. People try to make this too complicated, like I did above but its just treating the function inc(x) as a variable increment. Simple.

RUN

go run anonymous-function.go
go run return-function-to-function.go

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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