Define a recursive function within a function in Go

huangapple go评论93阅读模式
英文:

Define a recursive function within a function in Go

问题

我正在尝试在Go语言中在另一个函数内定义一个递归函数,但是我在获取正确的语法方面遇到了困难。我希望实现以下类似的结构:

func Function1(n int) int {
   a := 10
   Function2 := func(m int) int {
      if m <= a {
         return a
      }
      return Function2(m-1)
   }

   return Function2(n)
}

我希望将Function2保持在Function1的作用域内,因为它需要访问一些作用域内的元素。

在Go语言中,我应该如何实现这个功能?

非常感谢!

英文:

I am trying to define a recursive function within another function in Go but I am struggling to get the right syntax. I am looking for something like this:

func Function1(n) int {
   a := 10
   Function2 := func(m int) int {
      if m &lt;= a {
         return a
      }
      return Function2(m-1)
   }

   return Function2(n)
}

I'd like to keep Function2 inside the scope of Function1 as it is accessing some elements of its scope.

How can I do this in Go?

Many thanks

答案1

得分: 70

如果在声明Function2的那一行中,你无法在其中访问它。原因是你引用的不是一个函数,而是一个变量(其类型是函数),它只能在声明之后才能访问。

引用自规范:声明和作用域

在函数内部声明的常量或变量标识符的作用域从ConstSpec或VarSpec的末尾开始(对于短变量声明为ShortVarDecl),并在最内层的包含块的末尾结束。

在你的示例中,Function2是一个变量声明,VarSpec 如下:

Function2 := func(m int) int {
    if m <= a {
        return a
    }
    return Function2(m-1)
}

正如语言规范中的引用所描述的那样,变量标识符 Function2 只会在声明之后才在作用域内,因此你无法在声明本身内部引用它。有关详细信息,请参见这里

首先声明 Function2 变量,这样你就可以从函数字面量中引用它:

func Function1(n int) int {
    a := 10
    var Function2 func(m int) int

    Function2 = func(m int) int {
        if m <= a {
            return a
        }
        return Function2(m - 1)
    }

    return Function2(n)
}

Go Playground上尝试一下。

英文:

You can't access Function2 inside of it if it is in the line where you declare it. The reason is that you're not referring to a function but to a variable (whose type is a function) and it will be accessible only after the declaration.

Quoting from Spec: Declarations and scope:

> The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl for short variable declarations) and ends at the end of the innermost containing block.

In your example Function2 is a variable declaration, and the VarSpec is:

Function2 := func(m int) int {
    if m &lt;= a {
        return a
    }
    return Function2(m-1)
}

And as the quote form the language spec describes, the variable identifier Function2 will only be in scope after the declaration, so you can't refer to it inside the declaration itself. For details, see https://stackoverflow.com/questions/52503560/understanding-variable-scope-in-go/52506086#52506086.

Declare the Function2 variable first, so you can refer to it from the function literal:

func Function1(n int) int {
    a := 10
	var Function2 func(m int) int

    Function2 = func(m int) int {
		if m &lt;= a {
    		return a
		}
		return Function2(m - 1)
	}

    return Function2(n)
}

Try it on Go Playground.

答案2

得分: 7

var Function2 func(m int) int
Function2 = func(m int) int {
...

英文:
var Function2 func(m int) int
Function2 = func(m int) int {
    ...

huangapple
  • 本文由 发表于 2015年1月23日 05:58:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/28099441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定