Calling a function in Golang with a name

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Last week, I wrote "Function Call by name" in Golang. Because it is in English, so people are criticized (say who, who knows!) )。 OK, now write it again in Chinese.

The function in Golang, like C, is a block of code, but it can be assigned to a variable like any other type.

If you are unfamiliar with functions, Codewalk:first-class Functions in Go should be a good starting point. Already know something? Then go ahead!

First, take a look at this PHP code:

function Foobar () {    echo "Hello golang\n";} $funcs = Array (    "foobar" = "Foobar",    "Hello"  = "Foobar"), $funcs ["Foobar"] (); $funcs ["Hello"] ();

It will output:

mikespook@mikespook-laptop:~/desktop$ php foobar.php Hello golanghello Golang

Using this method to invoke a function that matches the name is very effective.

So, is it possible to invoke a function with the name of a function in Golang?

As a static, compiled language, the answer is no ... Yes, again!

In Golang, you can't do this:

Func Foobar () {    //Bla...bla...bla ...} FuncName: = "Foobar" FuncName ()

However, you can:

Func Foobar () {    //Bla...bla...bla ...} Funcs: = Map[string]func () {"Foobar": foobar}funcs["Foobar"] ()

But here's a limitation: This map can only be used as a function of "func ()" Without input parameters or return values.
If you want to use this method to implement functions that call different function prototypes, you need to use interface{}.

Oh, yes! interface{}, similar to the void pointer in C. Do you remember this thing? You don't remember? All right, check this out: The Go programming Language specification:interface types.

In this way, you can add functions with different function prototypes to a map:

Func foo () {    //Bla...bla...bla ...} Func Bar (A, b, C int) {    //Bla...bla...bla ...} Funcs: = map[string]interface{}{"foo": foo, "Bar": Bar}

So how do you call a function in map? Like this:

funcs["foo"] ()

Absolutely not! This can't work! You cannot directly invoke a function stored in an empty interface.

Reflection into our lives! In the Golang there is a package called "reflect". Do you know the reflection?
If not, read this: Laws of reflection. Oh, here's a Chinese version: the rules of Reflection.

Func call (M map[string]interface{}, name string, params ... interface{}) (Result []reflect. Value, err Error) {    F = reflect. ValueOf (M[name])    if Len (params)! = F.type (). Numin () {        err = errors. New ("The number of the params is not adapted.")        Return    } in    : = Make ([]reflect. Value, Len (params))    for k, param: = range params {        in[k] = reflect. ValueOf (param)    }    result = F.call (in)    Return}call (Funcs, ' foo ') call (Funcs, "Bar", 1, 2, 3)

reflect the value of the function from the empty interface, and then use reflect. Call to pass the parameter and invoke it.
Nothing is hard to understand.

I have completed a package to implement this feature, here: Https://bitbucket.org/mikespook/golib/src/27c65cdf8a77/funcmap.

I hope this helps a little. Good luck, Gopher!

Related Article

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.