通过反射, 简单的调用实例的方法。虽然这个反射性能不是很好, 但有时候用起来往往还挺爽的。

暴力直接上代码

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49

package main

import (
"log"
"reflect"
)

type Logic struct{}
type Query struct {
Id int
}

type Person struct {
Id int
Name string
}

func (l *Logic) Echo(q *Query) (*Person, error) {
return &Person{
Id: 1,
Name: "luowen",
}, nil
}

func main() {

l := &Logic{}
_, ok := reflect.TypeOf(l).MethodByName("Echo")
if !ok { // check method 'Echo' exists
log.Fatalf("instance method %s not exists!", "Echo")
}
// sm.Type.NumOut() // number of method return.
// sm.Type.NumIn() // number of method arguments need.
resp := reflect.ValueOf(l).MethodByName("Echo").Call([]reflect.Value{ // reflect invoke it
reflect.ValueOf(&Query{
Id: 10,
}),
})
if !resp[0].IsNil() { // get result
p := resp[0].Interface().(*Person)
log.Printf("result[0]: %d, %s\n", p.Id, p.Name)
}
if !resp[1].IsNil() { // get result
e := resp[1].Interface().(error)
log.Printf("result[1]: %v\n", e)
}
}