SoFunction
Updated on 2025-03-05

Definition and usage of Golang callback functions, closures and interface functions

Callback function

definition

A callback function passes the function's pointer (address) as a parameter to another function. When this pointer is used to call the function it points to, it is said to be a callback function.

eg: js interface callback function, gin handler

significance

Callback functions are a way for users to implement asynchronous: register the processing function as a routing callback function, and automatically call the callback function when there is a request; in this way, the execution of the main program will not be affected by the request and realize asynchronous. Of course, the asynchronous mechanism here is implemented by epoll and cannot be considered asynchronous in the strict sense.

Closure

definition

A closure refers to an entity composed of a function and a reference environment related to it; simply put, closure = function + reference environment.

A closure refers to a function that has permission to access variables in another function scope; a common way to create a closure is to create another function inside one function, and the inner function can access variables of the outer function.

eg:

func adder() func(x int)int{
    i:=10  //The local variable i in adder() can be accessed by fn and fn1, and i has its own value in each closure instance    return func()int{
        return i+x
    }
}
func main() {
    fn := adder()
    (fn(10))  //20
    (fn(10))  //30
    fn1 := adder()
    (fn1(10))  //20
    (fn1(10))  //30
}

significance

Global variables:

1. Resident memory

2. Pollution overall

Local variables:

1. Not permanently resident memory

2. Do not pollute the overall situation

And Go language closures:

1. You can make variables reside in memory

2. It can prevent variables from polluting the overall situation

Therefore, closures are mainly to avoid abuse of global variables.

Local variables returned by scope in closures will not be destroyed and recycled immediately, but excessive use of closures may occupy more memory, resulting in performance degradation.

Interface functions

definition

Function types implement a certain interface, called an interface-type function. Interface-type functions can only be applied to situations where only one method is defined within the interface.

significance

It is convenient for users to pass in functions as parameters when calling, and also in structures that implement the interface as parameters.

When an interface is used as a parameter: we can choose two interface implementation methods, interface function implements interface and structure implements interface.

When the logic is relatively complex, if the database operation requires a lot of information, address, username, password, and many intermediate states need to be maintained, such as timeout, reconnection, locking, etc. In this case, it is more suitable to encapsulate as a structure as a parameter.

When the logic is relatively simple, the interface function can be directly passed into.

func test(key string) ([]byte, error) {
	return []byte(key), nil
}
func main() {
    GetFromSource(GetterFunc(test), "hello")
}
//Will test Cast type to GetterFunc,GetterFunc Implemented the interface Getter,It is a legal parameter。This method is suitable for scenarios with relatively simple logic。

This allows you to use ordinary function types (need to type conversion) as parameters, and you can also use structures as parameters. It is more flexible and readable. This is the value of interface functions.

This is the article about the definition and use of Golang callback functions, closures and interface functions. For more related Golang callback functions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!