Home »
Golang »
Golang FAQ
Can closure be a recursive in Golang?
Learn, what is closure and recursive. Can closure be recursive in Golang?
Submitted by IncludeHelp, on October 05, 2021
Let's understand, what is a closure and recursive / recursion function, and can closure be recursive in Golang?
Yes, a closure can also be recursive, but this requires the closure to be declared with a typed var explicitly before it's defined.
Consider the below example,
package main
import "fmt"
func main() {
// Recursive closure function
var InfinitePrinting func()
InfinitePrinting = func() {
fmt.Println("Hello")
InfinitePrinting()
}
// The function calling
InfinitePrinting()
}
Output:
Hello
Hello
Hello
Hello
Hello
.
.
.
Infinite time
Golang FAQ »