A callback function is a special function notation that plays a broad role in many scenarios. But for beginners, the callback function is a headache of a thing, not too understood, I studied some, with an example on the web to explain the first look at a code example (from the Internet)
package mainimport"fmt"typefuncintintfunc main() { 12 fmt.Println(test(x, y, add))}//实现回调funcintint { return callback(x, y)}funcintint { return x + y}
This code runs the following results
3
- In the above example, the function test receives 3 parameters, the first two arguments are of type int, the third argument is the function type, and the return value is int, the result of this int is actually the result of callback (x, y), but callback (x, y) is implemented somewhere else, is implemented by the function add (x, y). This is the callback.
- The logic is as follows: first the program runs, to print the result of test (X,Y,ADD), the function test receives three parameters, the third parameter is passed to the function add, the return is callback (x, y), actually returns the Add (x, y). The Add (x, y) is defined by another place and returns the value of X+y. So ultimately, the result of the function main is to print out the X+y, which is 3
To summarize, go is a support function callback, we can pass the name of function A as a parameter to another function B, and then implement this function A in other places, so that the function callback
002_ parsing the callback function in the Go language