dongqian6554
dongqian6554
2012-08-01 19:37
浏览 1.0k
已采纳

Go是否具有lambda表达式或类似内容?

Does Go support lambda expressions or anything similar?

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

图片转代码服务由CSDN问答提供 功能建议

Go是否支持lambda表达式或类似的东西?

我要移植 一个使用lambda表达式(Ruby)的其他语言编写的库。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • dph19153
    dph19153 2012-08-01 19:44
    已采纳

    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
    ", foo())
    }
    
    func returnsAFunction()Stringy{
      return func()string{
        fmt.Printf("Inner stringy function
    ");
        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
    "
      };
      fmt.Printf(baz());
    }
    
    点赞 1 评论
  • duanpiangeng8958
    duanpiangeng8958 2012-08-02 17:35

    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/

    点赞 评论
  • douji0073
    douji0073 2017-07-21 06:38

    Yes

    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.

    Output
    $ go run closures.go
    1
    2
    3
    1
    
    点赞 评论
  • doukaizha5417
    doukaizha5417 2018-07-02 03:13

    Yes, but it is a lambda expression in its fullest form, closures and all. You don't get type inference with function literals however! That's what makes Go sucks!

    点赞 评论
  • doutang3077
    doutang3077 2018-08-05 01:24

    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.

    点赞 评论
  • doufeng1249
    doufeng1249 2019-01-13 16:40

    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:

    func() string {
        return "some String Value"
    }
    //Js similar: () => 'some String Value'
    

    with string args and return string

    func(arg string) string {
        return "some String" + arg
    }
    //Js similar: (arg) => "some String Value" + arg
    

    no arguments and no returns (void)

    func() {
       fmt.Println("Some String Value")
    } 
    //Js similar: () => {console.log("Some String Value")}
    

    with Arguments and no returns (void)

    func(arg string) {
        fmt.Println("Some String " + arg)
    }
    //Js: (arg) => {console.log("Some String Value" + arg)}
    
    点赞 评论
  • dpnfxk251524
    dpnfxk251524 2019-08-23 11:52

    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.

    test1, test2 := func() (string, string) {
        x := []string{"hello", "world"}
        return x[0], x[1]
    }()
    

    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.

    点赞 评论

相关推荐