Go language provides a special feature known as an anonymous function. An anonymous function can form a closure. A closure is a special type of anonymous function that references variables declared outside of the function itself. It is similar to accessing global variables which are available before the declaration of the function.
Example:
package main
import "fmt"
func main() {
GFG := 0
counter := func() int {
GFG += 1
return GFG
}
fmt.Println(counter())
fmt.Println(counter())
}
|
Output:
1
2
Explanation: The variable GFG was not passed as a parameter to the anonymous function but the function has access to it. In this example, there is a slight problem as any other function which will be defined in the main has access to the global variable GFG and it can change it without calling counter function. Thus closure also provides another aspect which is data isolation.
package main
import "fmt"
func newCounter() func() int {
GFG := 0
return func() int {
GFG += 1
return GFG
}
}
func main() {
counter := newCounter()
fmt.Println(counter())
fmt.Println(counter())
}
|
Output:
1
2
Explanation: The closure references the variable GFG even after the newCounter() function has finished running but no other code outside of the newCounter() function has access to this variable. This is how data persistency is maintained between function calls while also isolating the data from other code.
Last Updated :
23 Mar, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...