Value.Call()
返回函数调用为[]reflect.Value
的返回值。您可以使用Value.Interface()
方法将reflect.Value
表示的值作为interface{}
的值。从那里,您可以使用type assertion来获取不同类型的值。
像该简化的例子:
var funcList = map[string]interface{}{
"test": func(ctx context.Context, data interface{}) (interface{}, error) {
return "test-result-data:" + fmt.Sprint(data), errors.New("test-error")
},
}
func Send(funcName string, data interface{}) (interface{}, error) {
f := reflect.ValueOf(funcList[funcName])
params := []reflect.Value{
reflect.ValueOf(context.Background()),
reflect.ValueOf(data),
}
res := f.Call(params)
ret := res[0].Interface()
var err error
if v := res[1].Interface(); v != nil {
err = v.(error)
}
return ret, err
}
测试它:
result, err := Send("test", "testData")
fmt.Println(result, err)
输出:
test-result-data:testData test-error
但是,这是不必要的复杂。你并不需要使用反射来调用一个函数,你可以直接调用它,并将结果直接返回,这样的:
func Send2(funcName string, data interface{}) (interface{}, error) {
return funcList[funcName].(func(context.Context, interface{}) (interface{}, error))(
context.Background(), data,
)
}
测试它:
result, err = Send2("test", "testData")
fmt.Println(result, err)
输出是一样的。试试Go Playground上的示例。
你得到它们。代码中的'value'将是返回值的一部分。你有什么问题? – Adrian
如果您查看['Call'签名](https://golang.org/pkg/reflect/#Value.Call),您的'value'是'[] reflect.Value'。你甚至可以打印出来。究竟是什么问题? – JimB
想知道,为什么你使用反射,为什么不直接调用它,并返回结果,例如'funcName.String()]。(func(context.Context,interface {}))(interface {},error))(ctx,data)'' (只有在'funcList'中存储不同类型的函数时才需要类型断言;否则,您甚至可以摆脱它,并且它变为'return funcList [funcName.String()](ctx,data)'。) – icza