函数类型转换

How can I convert func add (a, b int) int to func(...interface{}) interace{} type ?

Any ideas about implementing generic functions using the reflect package ?

There is no "casting" is go (well, using the "unsafe" package kind of is like casting).

You cannot convert function types like this, since they have different layouts in memory. Generic-like functions can be made through the reflect package, though with significant overhead. See http://golang.org/pkg/reflect/#example_MakeFunc for an example.

For most use cases of generic functions, you're probably better off accepting an interface, and using type assertions or switches (http://golang.org/ref/spec#Type_switches), rather than the reflection library.

As JimB said, you can't cast in Go and you cannot convert functions just like that but by using closures, you can rapidly wrap your function:

func add(a, b int) int {
    return a + b;
}

wrap := func(args ...interface{}) interface{} {
    return interface{} (add(args[0].(int), args[1].(int)))
}

Note that wrap will panic if you give it arguments that are not of type int. If you want to avoid that you can slightly modify wrap:

wrap := func(args ...interface{}) (interface{}, error) {
    a, k := args[0].(int)
    b, l := args[1].(int)
    if !k || !l {
        return nil, errors.New("Arguments must be of type int")
    }
    return add(a,b), nil
}

If you'd like to do different things with wrap, depending on it's arguments types you can do so by using a type switch:

func addInts(a, b int) int {
    return a + b;
}

func addFloat64s(a, b float64) float64 {
    return a + b;
}

wrap := func(args ...interface{}) interface{} {
    switch args[0].(type) {
    case int: return interface{}(addInts(args[0].(int), args[1].(int)))
    case float64: return interface{}(addFloat64s(args[0].(float64), args[1].(float64)))
    }
}

Note that this last version of wrap makes the assumption that all given parameters will have the same type and at least 2 arguments are given.