Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use anonymous functions in Go language

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use anonymous functions in the Go language. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Anonymous functions in Go language

It refers to a function that does not need a name and is usually defined inside another function.

Format:

Func (parameter column other) (return value list) {

}

Example:

Package mainimport ("fmt") func main () {Sum: = func (l, rint) (sum int) {sum = l + r return} / / Note: variable Sum is used as an anonymous function, summing fmt.Println ("Hello, sum Address: sum!", & Sum, Sum (50J 10)) return} Output:Hello, sum Address: sum! 0x40c128 60

Use the scene:

Anonymous functions are usually short and do not want to be used outside of this function, similar to lamdba expressions in C++.

Supplementary note:

Anonymous functions can also be implemented as an array of functions, and users can call different anonymous functions according to different indx, so that users can use these anonymous functions flexibly according to their own needs.

Example:

Package mainimport ("fmt") func main () {funcs: = [] (func (str string) string) {/ / Note: there are three different functions func (str string) string {return "Format 1:" + str}, func (str string) string {return "Format 2:" + str}, func (str string) string {return "Format 3:" + str},} for idx, fun: = range funcs {fmt.Println ("Hello") Funcs idx, result: ", idx, fun (" World ")} return} output:Hello, funcs idx, result: 0 Format 1: WorldHello, funcs idx, result: 1 Format 2: WorldHello, funcs idx, result: 2 Format 3: World

II. Lambda expression in C++

The lambda expression in C++ appears after C++ 11. (note: only C++ 11 is introduced here) official documents say that it is a closure type, the author personally think that it can be understood as a function, but this function has the following characteristics.

1. Is the function within the function, the scope is inside the function, after the function, can not be used.

The function represented by the 2.lambda expression has no name.

3. Usually what this lambda expression does is short and short.

Why do we need lambda expressions?

The author thinks that the main reason for the existence of lambda expression is that part of the code inside the function has a high reuse rate, but it is only used within this function, and other functions will not be used.

This leads to an awkward situation, write as a separate function, the scope is a bit extensive, do not write as a function, the function has to write multiple copies of the same code, so the lambda expression is generated.

The compilation of lambda is compiled as a function, which is identified by the compiler knowing that it is a lambda expression when it sees [, and then compiling according to the rules of the lambda expression.

How do I use lambda expressions?

Format of 1.lambda expression

[captures] (params)-> ret {body} / / expression with parameters and return value

[captures] (params) {body} / / an expression with no return value

[captures] {body} / / expression with no parameters and no return value

Captures is called the trapper, params is the formal parameter, ret is the return type, and body represents the function body. (note: captures is more complex, described later, and the rest is easier to understand. )

The details of the trapper are as follows:

two。 Examples

1). [] / / No variable is defined. It is wrong to try to use any external variable within Lambda.

/ / Example program#include # include int main () {std::string name = "World!"; auto flag = [] (int a, int b)-> bool {return a < b;}; bool ok = false; ok = flag (2,3); std::cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report