这个问题在这里已有答案:
在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)
{
// ...
}
试试这个 :
func Test(n int, x string) {
fmt.Println(n, x)
}
func main() {
myFunc := func(x string) { Test(123, x) }
myFunc("hello")
}