Does Go support lambda expressions or anything similar?
I want to port a library from another language that uses lambda expressions (Ruby).
Does Go support lambda expressions or anything similar?
I want to port a library from another language that uses lambda expressions (Ruby).
Lambda expressions are also called function literals. Go supports them completely.
See the language spec: http://golang.org/ref/spec#Function_literals
See a code-walk, with examples and a description: http://golang.org/doc/codewalk/functions/
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.
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.
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 !!
no args return string:
with string args and return string
no arguments and no returns (void)
with Arguments and no returns (void)
Here is an example, copied and pasted carefully:
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.
An example that hasn't been provided yet that I was looking for is to assign values directly to variable/s from an anonymous function e.g.
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 anassignment mismatch: 2 variable but 1 values
error.