I have a function of the IndexController type:
func (this IndexController) ActionIndex() map[string]string {
return map[string]string{"Name": "Hello from the actionIndex()!"}
}
It is used so:
routerInstance := router.Constructor(request)
controllerObject := controllers[routerInstance.GetRequestController(true)]
outputData := reflect.ValueOf(controllerObject).MethodByName(routerInstance.GetRequestAction(true)).Call([]reflect.Value{})
fmt.Println(outputData)
Now for example, how to show the Name element of outputData ? I try to print so:
fmt.Println(outputData["Name"])
But program will exit with error:
# command-line-arguments
./server.go:28: non-integer array index "Name"
I will be thankful!
First, (v Value) Call(…)
returns a []reflect.Value
, so you need to index it to get your actual return value:
outputData := reflect.ValueOf(controllerObject).MethodByName(routerInstance.GetRequestAction(true)).Call([]reflect.Value{})[0]
(note the [0]
at the end of the line)
From there on, you could probably do a type switch or maybe an interface conversion like outputData.(map[string]interface{})
to get a map that you can index by string.
It would help though if you would provide your entire server.go
file, or maybe put it up on play.golang.org .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.