Go Closures: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 5: | Line 5: | ||
=Overview= | =Overview= | ||
A closure is an anonymous function declared within a block - and implicitly, within another function. The function continues to have access to the local variables it had access when it was created for the duration of its life. The function together with the non local variables it references is known as a ''closure''. | A closure is an [[anonymous function|Go Concepts - Functions#Anonymous_Functions]] declared within a block - and implicitly, within another function. The function continues to have access to the local variables it had access when it was created for the duration of its life. The function together with the non local variables it references is known as a ''closure''. | ||
<pre> | <pre> |
Revision as of 00:25, 3 April 2016
Internal
Overview
A closure is an Go Concepts - Functions#Anonymous_Functions declared within a block - and implicitly, within another function. The function continues to have access to the local variables it had access when it was created for the duration of its life. The function together with the non local variables it references is known as a closure.
inClosuresScope := 10 var c = func (i int) { inClosuresScope += i } s := []int {1, 2, 3, 4, 5} for _, value := range s { c(value); }
Alternatively, the function literal and invocation can be defined in-line:
inClosuresScope := 10 s := []int {1, 2, 3, 4, 5} for _, value := range s { func (i int) { inClosuresScope += i }(value); }
More about closures is available here.