Does Go have lambda expressions or anything similar?

前端 未结 6 1764
青春惊慌失措
青春惊慌失措 2020-12-08 03:36

Does Go support lambda expressions or anything similar?

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

6条回答
  •  萌比男神i
    2020-12-08 04:10

    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.

提交回复
热议问题