ADVERTISEMENT
ADVERTISEMENT

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 »

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.