GO语言中的Lambda表达式[重复]

问题描述 投票:-1回答:1

这个问题在这里已有答案:

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

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)
{
    // ...
}
go lambda
1个回答
3
投票

试试这个 :

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

playground

© www.soinside.com 2019 - 2024. All rights reserved.