Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Go have lambda expressions or anything similar?

Tags:

lambda

go

Does Go support lambda expressions or anything similar?

I want to port a library from another language that uses lambda expressions (Ruby).

like image 671
loyalflow Avatar asked Aug 01 '12 19:08

loyalflow


People also ask

Does Golang have lambda?

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.

Is Golang good for AWS Lambda?

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.

What can I use instead of lambda?

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.

Are lambda expressions like Arrow functions?

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.


1 Answers

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()); } 
like image 173
perreal Avatar answered Sep 30 '22 19:09

perreal