-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_func.go
35 lines (29 loc) · 879 Bytes
/
make_func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package test
import (
"fmt"
"reflect"
)
func Sum(args []reflect.Value) []reflect.Value {
a, b := args[0], args[1]
if a.Kind() != b.Kind() {
fmt.Println("types of elements are not the same")
return nil
}
switch a.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return []reflect.Value{reflect.ValueOf(a.Int() + b.Int())}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return []reflect.Value{reflect.ValueOf(a.Uint() + b.Uint())}
case reflect.Float32, reflect.Float64:
return []reflect.Value{reflect.ValueOf(a.Float() + b.Float())}
case reflect.String:
return []reflect.Value{reflect.ValueOf(a.String() + b.String())}
default:
return []reflect.Value{}
}
}
func MakeSum(fptr interface{}) {
fn := reflect.ValueOf(fptr).Elem()
v := reflect.MakeFunc(fn.Type(), Sum)
fn.Set(v)
}