002_ parsing the callback function in the Go language

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.