In computer programming, an anonymous function or lambda abstraction (function literal) is a function definition that is not bound to an identifier, and Go supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it.
package main
import "fmt"
func intSeq() func() int {
i := 0
return func() int {
i += 1
return i
}
}
func main() {
nextInt := intSeq()
fmt.Println(nextInt())
fmt.Println(nextInt())
fmt.Println(nextInt())
newInts := intSeq()
fmt.Println(newInts())
}
function intSeq returns another function, which we define anonymously in the body of intSeq. The returned function closes over the variable i to form a closure.
Yes, since it is a fully functional language, but has no fat arrow (=>) or thin arrow (->) as the usual lambda sign, and uses the func keyword for the sake of clarity and simplicity.
The golang does not seem to make lambda expressions, but you can use a literal anonymous function, I wrote some examples when I was studying comparing the equivalent in JS, I hope it helps !!
Note: you require brackets () at the end of the function to execute it and return the values otherwise only the function is returned and produces an assignment mismatch: 2 variable but 1 values error.
Here is a 'curried function' example. However the syntax seems unclear, relative to the lambda function syntax in other languages such as Swift, C#, etc.
func main() int {
var f func(string) func(string) int
f = func(_x1 string) func(string) int { return func(_x2 string) int { return strings.Compare(_x1,_x2) } }
return ((f)("b"))("a")
}