Updated April 14, 2023
Definition of Golang Closure
Closure is a function in go language which perform all the things perform by any function, the only difference is that closure function does not have any names and accessibility of the variables, it is good in case if the calculation is going to append inside the function and it will also save the memory for us because you know every variable take some space in the programing language but we can see we are not giving any name to closure function so it will not occupy any space until it will get a call for performing its duty and calculations if we want then we can assign the closure function to any variable and can call the function with the variable name.
Syntax
In below we have given a simple example of the syntax for th go closure, we can explain the below syntax in the following steps,
- First, we have written func and then we are passing some params to it with the datatype of the params.
- It is very much similar to any other function, the only difference we can see here that it does not have any name.
- Another way is to assign this closure function to any variable, lille x=funct().
- Once we assign the closure function to any variable we can call that function with the name of the variable name.
func(data data-type) data-type {
//expression 1
//expression 2
//expression 3
}
How does Closure Work in Go Language?
Before going to discuss the working of the closure in the go language we need to understand why it was introduced. Actually many times for doing any calculation inside the function we perform several calculations and many times the calculations go very long and lengthy so to deal with such a lengthy calculation they introduced the closure concept. You maybe think why we do not use the concept of the function instead of the closure, so answer it allows us to assign the closure function to any variable and we can create many variables inside any functions with many names and we can do call to those variables. We can explain working with some of the important points.
- Closure in the go language is not any predefined function or package rather it is a way to perform functional calculations.
- It allows us to use a function without giving it any names.
- We can write any function as an anonymous function which means any function without any names.
- Suppose we have a function x and inside function x we have defined any closure function then to call that closure function we can simply first get the x into another variable like var a =x() and then we can call the closure function places inside the x with a(). Here when we write the a() , which means we are notifying to the compiler that call the function places as the closure inside the function x.
- It gives us the flexibility to handle many small calculations without creating a function for each, it also allows us to save some memory.
- We will see all these works in the examples section, where we are performing the calling of the closure functions.
Examples of Golang Closure
In below we have given some of the important examples of the go closure, in examples, we are calling the functions with a various ways for any closure. It’s simple and strait examples where we are creating some variable from the anonymous function (closure function) and calling that variable with the name of the variable. We can also pass some parameters to the closure function in the same way how we do in the case of the normal function. In case if we want to see and test the examples then we can create a file with the name closur.go and copy and paste the below example and run the command go run closure.go and we can see the outputs.
Example #1
Code:
//Importing the main function here
package main
import "fmt"
//The function main is the the function which will called for the first time of the execution
func main() {
//Initialising the TEST variable for further uses
TEST := 0
//A closure function without any name performing addition on each call
cnt := func(a int) int {
TEST = 2 + a
return TEST
}
//calling the function containing the closure function .
fmt.Println("The output of the closure function is",cnt(2))
fmt.Println("The output of the closure function is",cnt(3))
fmt.Println("The output of the closure function is",cnt(4))
}
Output:
Example #2
Code:
//Importing the main function here
package main
import "fmt"
func addition() func(int) int {
//Initialising the add variable for further uses
add := 0
//A closure function without any name performing addition on each call
return func(a int) int {
add += a
return add
}
}
//The function main is the the function which will called for the first time of the execution
func main() {
//calling the function containing the closure function .
positive, negative := addition(), addition()
for k := 0; k < 11; k++ {
fmt.Println(
positive(k),
negative(-3*k),
)
}
}
Output:
Example #3
Code:
//Importing the main function here
package main
import "fmt"
func startCounter() func(a int) int {
//Initialising the TEST variable for further uses
TEST := 1
//A closure function without any name performing addition on each call
return func(a int) int {
TEST = TEST+a
return TEST
}
}
//The function main is the the function which will called for the first time of the execution
func main() {
//calling the function containing the closure function .
cnt := startCounter()
//Here in the below we are calling the closure functions with the variable name which we created from the function cal .
fmt.Println("The counter is",cnt(4))
fmt.Println("The counter is",cnt(5))
fmt.Println("The counter is",cnt(6))
fmt.Println("The counter is",cnt(7))
}
Output:
Recommended Articles
This is a guide to Golang Closure. Here we discuss the definition, syntax, and working of closure in go language along with different examples and its code implementation. You may also have a look at the following articles to learn more –