GO语言中的Lambda表达式

时间:2017-12-29 07:35:11

标签: go lambda

在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)
{
    // ...
}

1 个答案:

答案 0 :(得分:3)

试试这个:

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

playground

相关问题