Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recurse a closure in Go? [duplicate]

How to recurse a closure in Go?

Suppose I have a closure like

recur := func(){
    recur()
}

Compiler says:

undefined: recur

How can i implement it? Why is it happening?

like image 817
Ezequiel Moreno Avatar asked Dec 10 '15 06:12

Ezequiel Moreno


1 Answers

it happens because of how the order of evaluation works.

As of December 2015 (go.1.5.1), there isn't any language feature providing it.

Possible workarounds:

var recur func()
recur = func(){
    recur()
}

//or

type recurF func(recurF)

recur := func(recur recurF) {
    recur(recur)
}

More Info: https://github.com/golang/go/issues/226

like image 175
Ezequiel Moreno Avatar answered Nov 08 '22 22:11

Ezequiel Moreno