在 TCP 编程中, 很常见要根据客户端发送过来的自定义协议包, 通过解析协议包的指定部分信息, (一般为字符串) 来调用指定包中的指定方法。如何优雅的调用呢?

使用 golang 反射实现它!

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
50

// {GOPATH}/handles/chat.go

package handle

import "fmt"

type PingReq struct {
Message string
SendAt int
}

type Handle struct {
}

func New() *Handle {
return new(Handle)
}

func (h *Handle) Chat (pr PingReq) {
fmt.Println(pr)
}



// {GOPATH}/labcode/main.go

package main

import (
"labcode/handles"
"reflect"
)

func main() {
hd := handle.New()
hdv := reflect.ValueOf(hd)
chatMethed := hdv.MethodByName("Chat") // 反射出方法
if !chatMethed.IsValid() {
return
}
abc := handle.PingReq{
Message: "Wen Lo",
SendAt: 121212,
}
param := []reflect.Value{reflect.ValueOf(abc)} // 构造参数
chatMethed.Call(param) // 调用参数
}


**这样就能根据 Chat 这个字符串调用上了 Chat 方法