golang function closure

package main

import "fmt"

func main() {
    // Step 1: Declare a function that returns another function (closure).
    getClosure := func() func() int {
        // Step 2: Declare a variable within the outer function.
        count := 0

        // Step 3: Return a closure (inner function) that captures the outer variable.
        return func() int {
            // Step 4: Access and modify the outer variable within the closure.
            count++
            return count
        }
    }

    // Step 5: Call the outer function to get a closure.
    counter := getClosure()

    // Step 6: Call the closure to observe its behavior.
    fmt.Println(counter()) // Output: 1
    fmt.Println(counter()) // Output: 2
}