This question already has answers here:
Define a recursive function within a function in Go

(2个答案)


在8个月前关闭。




我在主函数内部有一个名为test的函数。
//stuff

func main() {
  var test = func() {
    if (/*some condition from main*/) {
      return test()
    }
  }

  val := test()
}

当我运行它说:

未定义:测试

它引用了测试功能内部的return test()
我怎样才能解决这个问题?

最佳答案

您必须先声明变量,然后才能使用它:

var test func()
test=func() {
   if ... {
     test()
   }
}

关于function - 当函数是变量时,重新运行当前函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60243111/

10-11 15:11
查看更多