SoFunction
Updated on 2025-03-04

Detailed explanation of the definition of anonymous functions and usage examples for the introduction to go syntax

1. Introduction

Go supports anonymous functions, that is, when you need to use functions, anonymous functions do not have function names but only function bodies. Functions can be assigned to variables of function types as a type. Anonymous functions are often passed in variables, which is similar to the callback functions in C. The difference is that Go supports defining anonymous functions in code at any time.

Anonymous functions refer to a function implementation method that does not need to define a function name. It consists of a function declaration and function body without a function name. Let’s introduce the definition and use of anonymous functions in detail below.

1.1 Definition of anonymous functions

fun(parameter)return{
    Function body
}

The definition of anonymous functions is an ordinary function definition without a name.

1.2 Definition of two ways of anonymous functions

Anonymous functions have no function names, so they cannot be called like ordinary functions. Therefore, anonymous functions need to be saved to a variable or executed immediately:

  • Save to a variable
  • Execute the function immediately
package main
import "fmt"
func main()  {
    // Define anonymous function method one    add := func (x, y int)  {
        (x + y)
    }
    add(1, 2)  // Call anonymous functions through variables    // Define anonymous function method two    func (x, y int)  {
        (x - y)
    }(20, 5)  //Self-execution function: After defining anonymous function, add () to execute directly}

1.3 Two scenarios of anonymous functions (callback function, closure)

1.3.1 Callback Function

func callFunc(base int, f func(int, int) int) {
    num := f(base, 5) // Reduce or add 5    ("base %d The value after f is: %d \n", base, num)
}
func TestCallFunc(t *) {
    addNum := func(base, num int) int {
        return base + num
    }
    callFunc(100, addNum)
}

Output:

=== RUN   TestCallFunc
base 100 The value after f is: 105
--- PASS: TestCallFunc (0.00s)

1.3.2 Closure

func TestClosure(t *) {
    var j int = 1
    f := func() {
        var i int = 1
        ("i, j: %d, %d\n", i, j)
    }
    f()
    j += 2
    f()
}

Output:

=== RUN   TestClosure
i, j: 1, 1
i, j: 1, 3
--- PASS: TestClosure (0.00s)

In the above example, the anonymous function refers to an external variable, so it is also a closure. The closure pointed to by the variable f refers to the local variables i and j. i is defined inside the closure, its value is isolated and cannot be modified from the outside, while the variable j is defined outside the closure, so it can be modified from the outside, and the closure only holds its reference.

The above is the detailed explanation of the definition and usage examples of the anonymous function in the introduction to go syntax. For more information about the anonymous function in the introduction to go syntax, please pay attention to my other related articles!