如何在Go中递归关闭? [重复]

This question already has an answer here:

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?

</div>

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