Code with Go
Ask or search…
K
Links

Closure

This Go program demonstrates the use of closures by defining a function that returns another function.
// Importing the "fmt" package, which provides functions for formatted I/O.
import "fmt"
// Function intSeq returns a closure: a function that "closes over" the variable i.
// The returned function can be used to generate a sequence of increasing integers.
func intSeq() func() int {
// Initialize a variable i.
i := 0
// Define and return an anonymous function (closure) that increments and returns i.
return func() int {
i++
return i
}
}
// The main function, which serves as the entry point for the program.
func main() {
// Call intSeq, which returns a function that will generate a sequence of integers.
nextInt := intSeq()
// Call the returned function, printing the next value in the sequence.
fmt.Println(nextInt())
// Call the returned function again, printing the next value in the sequence.
fmt.Println(nextInt())
// Call the returned function once more, printing the next value in the sequence.
fmt.Println(nextInt())
// Call intSeq again, creating a new closure with its own "i" variable.
newInts := intSeq()
// Call the new closure, printing the first value in its own sequence.
fmt.Println(newInts())
}

Output

1
2
3
1
Now, let's break down the code and explain each part:
  1. 1.
    Function Declarations:
    • intSeq() func() int: This function returns another function (a closure) that generates a sequence of increasing integers. The returned function "closes over" the variable i.
  2. 2.
    Main Function:
    • main(): This is the entry point of the program.
    • nextInt := intSeq(): Calls intSeq, which returns a closure (a function that increments and returns i). The closure is assigned to the variable nextInt.
    • fmt.Println(nextInt()): Calls the closure (stored in nextInt), printing the next value in the sequence.
    • fmt.Println(nextInt()): Calls the closure again, printing the next value in the sequence.
    • fmt.Println(nextInt()): Calls the closure once more, printing the next value in the sequence.
    • newInts := intSeq(): Calls intSeq again, creating a new closure with its own i variable.
    • fmt.Println(newInts()): Calls the new closure, printing the first value in its own sequence.