【发布时间】:2017-06-15 11:25:33
【问题描述】:
这是一个代码sn-p -
type Gateway struct {
Svc1 svc1.Interface
Svc2 svc2.Interface
}
func (g *Gateway) GetClient(service string) interface{} {
ps := reflect.ValueOf(g)
s := ps.Elem()
f := s.FieldByName(strings.Title(service))
return f.Interface()
}
func (g *Gateway) Invoke(service string, endpoint string, args...
interface{}) []reflect.Value {
log.Info("Gateway.Invoke " + service + "." + endpoint)
inputs := make([]reflect.Value, len(args))
for i, _ := range args {
inputs[i] = reflect.ValueOf(args[i])
}
client := g.GetClient(service)
return reflect.ValueOf(client).Elem().MethodByName(endpoint).Call(inputs)
}
GetClient("svc1") 工作正常。
但是,当我调用 Invoke("svc1", "endpoint1", someArg) 时,它会惊慌失措地说 -
reflect: call of reflect.Value.Elem on struct Value
reflect.ValueOf(client).MethodByName(endpoint).Call(inputs) 恐慌说 Call on a zero value.
【问题讨论】:
-
我为此尝试了解决方案 - stackoverflow.com/questions/14116840/…,我正在调用 Invoke("svc1", "Endpoint1", someArgs)。现在它没有崩溃,但也找不到 Endpoint1 方法。但是,如果我对 GetClient 的结果进行类型断言并调用 Endpoint1,那效果很好。
标签: go go-reflect