例子router.Get(path, handler) // works finemethodStr = "Get" router.methodStr(path, handler) // errorfuncs := map[string]func(){"methodStr": "Get"}router.funcs["methodStr"](path, handler) // errorreflect.ValueOf(router).MethodByName("Get").Call([]reflect.Value{}) // error我將方法名稱作為字符串獲取。如何使用字符串名稱調用路由器對象方法
1 回答

qq_花開花謝_0
TA貢獻1835條經驗 獲得超7個贊
您遇到的前兩個錯誤不是有效的 Go,所以我不確定您對它們的期望。最后一個帶有reflect的示例沒有任何需要2的函數的參數,這會導致恐慌。添加 2 個參數工作正常:
http://play.golang.org/p/mSziWdW0hn
args := []reflect.Value{
reflect.ValueOf("path"),
reflect.ValueOf("handler"),
}
reflect.ValueOf(router).MethodByName("Get").Call(args)
- 1 回答
- 0 關注
- 214 瀏覽
添加回答
舉報
0/150
提交
取消