Skip to content

Commit dc7e436

Browse files
szyhfantonmedv
authored andcommitted
Improve the reflect of getFunc(...)
1 parent c807621 commit dc7e436

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

runtime.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func extract(val interface{}, i interface{}) (interface{}, bool) {
8383
return nil, false
8484
}
8585

86-
func getFunc(val interface{}, i interface{}) (interface{}, bool) {
86+
func getFunc(val interface{}, name string) (interface{}, bool) {
8787
v := reflect.ValueOf(val)
8888
d := v
8989
if v.Kind() == reflect.Ptr {
@@ -92,20 +92,18 @@ func getFunc(val interface{}, i interface{}) (interface{}, bool) {
9292

9393
switch d.Kind() {
9494
case reflect.Map:
95-
value := d.MapIndex(reflect.ValueOf(i))
95+
value := d.MapIndex(reflect.ValueOf(name))
9696
if value.IsValid() && value.CanInterface() {
9797
return value.Interface(), true
9898
}
9999
// A map may have method too.
100100
if v.NumMethod() > 0 {
101-
name := reflect.ValueOf(i).String()
102101
method := v.MethodByName(name)
103102
if method.IsValid() && method.CanInterface() {
104103
return method.Interface(), true
105104
}
106105
}
107106
case reflect.Struct:
108-
name := reflect.ValueOf(i).String()
109107
method := v.MethodByName(name)
110108
if method.IsValid() && method.CanInterface() {
111109
return method.Interface(), true

0 commit comments

Comments
 (0)