×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Slices

Go Maps

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.