【问题标题】:Lambda Expressions in GO Language [duplicate]GO 语言中的 Lambda 表达式
【发布时间】:2017-12-29 07:35:10
【问题描述】:

在 C# 中,我们可以使用 lambda 表达式编写类似以下的内容,我们如何在 GO 语言中实现这一点?基本上,我正在寻找一种将一些参数传递给函数的能力,然后在它们可用时将一些参数传递给函数。

myFunc = (x) => Test(123, x) // Method Test is declared below.
myFunc("hello") // this calls method Test with params int 123 & string "hello" where int was passed upfront whereas string was passed when Test is actually called on this line

void Test(int n, string x)
{
    // ...
}

【问题讨论】:

  • 我认为这个问题已经回答了here

标签: go lambda


【解决方案1】:

试试这个:

func Test(n int, x string) {
    fmt.Println(n, x)
}
func main() {
    myFunc := func(x string) { Test(123, x) }
    myFunc("hello")
}

playground

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    相关资源
    最近更新 更多