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())
}
1
2
3
1
Now, let's break down the code and explain each part:
-
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 variablei
.
-
Main Function:
-
main()
: This is the entry point of the program. -
nextInt := intSeq()
: CallsintSeq
, which returns a closure (a function that increments and returnsi
). The closure is assigned to the variablenextInt
. -
fmt.Println(nextInt())
: Calls the closure (stored innextInt
), 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()
: CallsintSeq
again, creating a new closure with its owni
variable. -
fmt.Println(newInts())
: Calls the new closure, printing the first value in its own sequence.
-