Does Go support lambda expressions or anything similar?
I want to port a library from another language that uses lambda expressions (Ruby).
So does golang have lambda functions, YES golang have function literals that serve the same functionality as lambda expressions. These function literals are anonymous functions that do not have a name and can be defined inline i.e inside of other functions.
Advantages of Go (Golang) for AWS Lambda While all Lambda runtimes offer the same advantages in terms of scalability and share many concepts, there are some notable advantages when you use Go: Runtime versioning scheme, cold start performance, and pricing.
If you need to assign the lambda to a name, use a def instead. def s are just syntactic sugar for an assignment, so the result is the same, and they are a lot more flexible and readable.
JavaScript arrow functions are roughly the equivalent of lambda functions in python or blocks in Ruby. These are anonymous functions with their own special syntax that accept a fixed number of arguments, and operate in the context of their enclosing scope - ie the function or other code where they are defined.
Yes.
Here is an example, copied and pasted carefully:
package main import fmt "fmt" type Stringy func() string func foo() string{ return "Stringy function" } func takesAFunction(foo Stringy){ fmt.Printf("takesAFunction: %v\n", foo()) } func returnsAFunction()Stringy{ return func()string{ fmt.Printf("Inner stringy function\n"); return "bar" // have to return a string to be stringy } } func main(){ takesAFunction(foo); var f Stringy = returnsAFunction(); f(); var baz Stringy = func()string{ return "anonymous stringy\n" }; fmt.Printf(baz()); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With