A closure is a function that references variables from outside its body. The function "closes over" these variables and can access them even after the outer function returns:
func counter() func() int {
count := 0
return func() int {
count++
return count
}
}
Each call to counter() creates a new count variable. The returned function remembers and modifies its own count. Closures let you create functions with persistent state.