What is the method of defining anonymous functions in go language?

青灯夜游
Release: 2023-01-06 19:37:13
Original
3708 people have browsed it

The anonymous function definition method in the Go language is "func (parameter list) (return parameter list) {function body}". In fact, the definition of an anonymous function is an ordinary function definition without a name. An anonymous function can be completely regarded as a type, can be assigned directly, can be assigned to a variable, can be used as an actual parameter or return value, and of course can also be called directly.

What is the method of defining anonymous functions in go language?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Go language supports anonymous functions, that is, you define a function when you need to use it. Anonymous functions do not have function names, only function bodies. Functions can be assigned to variables of function type as a type. Anonymous functions are often also named Passed in the form of variables, this is similar to the callback function in C language. The difference is that Go language supports defining anonymous functions in the code at any time.

Anonymous function refers to a function implementation method that does not require the definition of a function name. It consists of a function declaration and a function body without a function name. The following is an introduction to how to define an anonymous function.

Define an anonymous function

The definition format of an anonymous function is as follows:

func(参数列表)(返回参数列表){
    函数体
}
Copy after login

The definition of an anonymous function has no name Ordinary function definition.

Basic usage

Anonymous function can be regarded as a type, can be directly assigned, can be assigned to variables, and can be used as actual parameters Or return the value for use, of course you can also call it directly

1) Call the anonymous function when defining

Anonymous functions can be called after declaration, for example:

func(data int) {
    fmt.Println("hello", data)
}(100)
Copy after login

Note the (100) after the } in line 3, indicating that the anonymous function is called and the parameter passed is 100.

2) Assign anonymous functions to variables

Anonymous functions can be assigned values, for example:

// 将匿名函数体保存到f()中
f := func(data int) {
    fmt.Println("hello", data)
}
// 使用f()调用
f(100)
Copy after login

Anonymous functions are very versatile and themselves It is a value that can be easily stored in various containers to implement callback functions and operation encapsulation.

Where to use anonymous functions

Callback function

Why use callback function? , let’s analyze it. A is the main function, and B is passed to the main function as a parameter. We can see from the above example that when we define the main function, we only define the parameters and return value type of function B. Then as long as the function B satisfies the type we defined and can be processed by function A, so using the callback function can make our main function more versatile. In layman's terms, the main function provides an interface, and any function that conforms to the rules of this interface can be used.

type Callback func(a, b int) int

// 提供接口,外部随便怎么搞,只要符合规则就行
func api(x,y int, callback Callback) int {
    return callback(x, y)
}

// 回调函数,只要满足传入参数类型为int
// 并且返回值类型也是int就都可以
func add(a, b int) int {
    return a + b
}
Copy after login

Closure

What is a closure? A closure is an entity composed of a function and its related reference environment.

func add() func(int) int {
    var b int
    return func(a int) int {
        b = b + a
        return b
    }
}

var f = add()
Copy after login

Here f is a closure, f saves a reference to b. Simply put, there is a pointer in f pointing to the address of b, so we can get the following results

fmt.Println(f(1)) // 由于b初始化为0,所以输出1
fmt.Println(f(2)) // 由于上一行代码已经修改了b的值,所以输出4
Copy after login

So Speaking of closure is equivalent to using anonymous functions inside our functions to handle all things related to variables

[Related recommendations: Go video tutorial, Programming teaching]

The above is the detailed content of What is the method of defining anonymous functions in go language?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!