Function call by name in Golang

The golang’s function is a code block like C’s, but it can also be assigned to a variable as its other types.

If you are not familiar with the function, Codewalk: First-Class Functions in Go should be a good starting point for you. Already known it? Let’s go on.

First of all, look at this PHP codes:

<br />
function foobar() {<br />
    echo &quot;Hello Golang\n&quot;;<br />
}<br />
$funcs = array(<br />
    &quot;foobar&quot; =&gt; &quot;foobar&quot;,<br />
    &quot;hello&quot;  =&gt; &quot;foobar&quot;,<br />
);<br />
$funcs[&quot;foobar&quot;]();<br />
$funcs[&quot;hello&quot;]();<br />

It will print:

<br />
[email protected]:~/Desktop$ php foobar.php<br />
Hello Golang<br />
Hello Golang<br />

It is very useful for calling a function with name matching.

So, is it possible to call a function by its name in Golang?
As a static, compiled programming language, the answer is No … and YES!

You can not do this in Golang:

<br />
func foobar() {<br />
    // bla...bla...bla...<br />
}<br />
funcname := &quot;foobar&quot;<br />
funcname()<br />

You can do:

<br />
func foobar() {<br />
    // bla...bla...bla...<br />
}<br />
funcs := map[string]func() {&quot;foobar&quot;:foobar}<br />
funcs[&quot;foobar&quot;]()<br />

But here’s a limitation that the map only work with the prototype “func()”, no input parameters and return arguments.
If you want to call some functions have different function’s prototypes, the interface{} should be used.

Yep! interface{}, like the void pointer in C. Remember that? No? Never mind! Read this: The Go Programming Language Specification:Interface types.

Then we could add functions with different prototypes into one map:

<br />
func foo() {<br />
    // bla...bla...bla...<br />
}<br />
func bar(a, b, c int) {<br />
    // bla...bla...bla...<br />
}<br />
funcs := map[string]interface{}{&quot;foo&quot;:foo, &quot;bar&quot;:bar}<br />

How to call a function in the map? like this?

<br />
funcs[&quot;foo&quot;]()<br />

NO! It does not work! You can not call a function stored in a empty interface variable directly.
Dadadada…

Reflection comes to us! It is a package called “reflect” in Golang. Do you know reflection already?
If not, just read this: Laws of reflection.

<br />
func Call(m map[string]interface{}, name string, params ... interface{}) (result []reflect.Value, err error) {<br />
    f = reflect.ValueOf(m[name])<br />
    if len(params) != f.Type().NumIn() {<br />
        err = errors.New(&quot;The number of params is not adapted.&quot;)<br />
        return<br />
    }<br />
    in := make([]reflect.Value, len(params))<br />
    for k, param := range params {<br />
        in[k] = reflect.ValueOf(param)<br />
    }<br />
    result = f.Call(in)<br />
    return<br />
}<br />
Call(funcs, &quot;foo&quot;)<br />
Call(funcs, &quot;bar&quot;, 1, 2, 3)<br />

Reflecting the function variable, use reflect.Call to call it and pass parameters into it at the same time.
Nothing could be hard to understand.

I’ve done a package for this functional: https://bitbucket.org/mikespook/golib/src/27c65cdf8a77/funcmap.

Hope this helps. Have a good time, gophers!

10 thoughts on “Function call by name in Golang”

  1. 所有涉及到go的函数均应该为func关键字而非function:比如:
    function foo() {
    // bla…bla…bla…
    }
    function bar(a, b, c int) {
    // bla…bla…bla…
    }

  2. How about:

    package mypackage

    func foo (s string, i int) (bool, error) {
    return false, nil
    }

    func bar (opts []string) (int, error) {
    return 456, nil
    }

    var Foo func(string, int) (bool, error)
    var Bar func([]string) (int, error)

    func init() {
    Foo = foo
    Bar = bar
    }

    func main() {
    b, err := Foo(“abc”, 345)
    i, err := Bar([]string{“abc”, “edf”})
    }

    You could use this for exporting private functions only for testing f.i..

    Johan

  3. Possible func variable value default?

    func foo (s string = “test”, i int = 1) (bool, error)

    How to do? its not work

  4. There is no default variable value for functions in Golang. If you want to do so, the `reflect` package may be the only choice for you. You can write a warp function which detects if the params are zero value, and then set a default value.

  5. Good article. I’m getting this error in your last example:
    cannot use funcs (type map[string]func()) as type map[string]interface {} in argument to Call

  6. https://gist.github.com/yaxinr/679976f1cde2a7b2925c30fa89b03eea
    // Invoke – firstResult, err := invoke(AnyStructInterface, MethodName, Params…)
    func Invoke(any interface{}, name string, args …interface{}) (reflect.Value, error) {
    method := reflect.ValueOf(any).MethodByName(name)
    methodType := method.Type()
    numIn := methodType.NumIn()
    if numIn > len(args) {
    return reflect.ValueOf(nil), fmt.Errorf(“Method %s must have minimum %d params. Have %d”, name, numIn, len(args))
    }
    if numIn != len(args) && !methodType.IsVariadic(){
    return reflect.ValueOf(nil), fmt.Errorf(“Method %s must have %d params. Have %d”, name, numIn, len(args))
    }
    in := make([]reflect.Value, len(args))
    for i := 0; i = numIn-1 {
    inType = methodType.In(numIn – 1).Elem()
    } else {
    inType = methodType.In(i)
    }
    argValue := reflect.ValueOf(args[i])
    argType := argValue.Type()
    if argType.ConvertibleTo(inType) {
    in[i] = argValue.Convert(inType)
    } else {
    return reflect.ValueOf(nil), fmt.Errorf(“Method %s. Param[%d] must be %s. Have %s”, name, i, inType, argType)
    }
    }
    return method.Call(in)[0], nil
    }

  7. Thanks! This is the way to call methods of a struct by name.
    However, it won’t work with normal functions.
    BTW, the last return statement `method.Call(in)[0]`, the element length should be checked if equals 0 or not.

Leave a Reply

Your email address will not be published. Required fields are marked *