无法通过反射调用的方法获取返回字符串值。

huangapple go评论122阅读模式
英文:

Can't get returning string value of a method called via reflection

问题

无法通过反射调用的方法获取返回的字符串值

恐慌:接口转换:接口是[]reflect.Value,而不是字符串

  1. package main
  2. import (
  3. "reflect"
  4. )
  5. type API_EndPoint struct{}
  6. func main() {
  7. var ep API_EndPoint
  8. s := reflect.ValueOf(&ep).MethodByName("EndPoint_X").Call([]reflect.Value{reflect.ValueOf(`foo bar`)})
  9. v := reflect.ValueOf(s)
  10. i := v.Interface()
  11. a := i.(string)
  12. println(a)
  13. }
  14. func (ep *API_EndPoint) EndPoint_X(params string) string {
  15. return "this_is_a_string" + params
  16. }

在play.golang.org上查看此代码

英文:

Can't get returning string value of a method called via reflection

panic: interface conversion: interface is []reflect.Value, not string

  1. package main
  2. import (
  3. "reflect"
  4. )
  5. type API_EndPoint struct{}
  6. func main() {
  7. var ep API_EndPoint
  8. s := reflect.ValueOf(&ep).MethodByName("EndPoint_X").Call([]reflect.Value{reflect.ValueOf(`foo bar`)})
  9. v := reflect.ValueOf(s)
  10. i := v.Interface()
  11. a := i.(string)
  12. println(a)
  13. }
  14. func (ep *API_EndPoint) EndPoint_X(params string) string {
  15. return "this_is_a_string" + params
  16. }

<iframe src="https://play.golang.org/p/3yJ5jAb9-2" frameborder="0" style="width: 100%; height: 100%"><a href="https://play.golang.org/p/3yJ5jAb9-2">see this code in play.golang.org</a></iframe>

答案1

得分: 2

.Call返回一个reflect.Value的切片,所以为了实现你想要的效果,你需要像这样做:

  1. package main
  2. import "reflect"
  3. type API_EndPoint struct {}
  4. func main() {
  5. var ep API_EndPoint
  6. s := reflect.ValueOf(&ep).MethodByName("EndPoint_X").Call([]reflect.Value{reflect.ValueOf("foo bar")})
  7. v := reflect.ValueOf(s)
  8. i := v.Interface()
  9. a := i.([]reflect.Value)[0].String() // 获取切片的第一个索引,并调用它的.String()方法
  10. println(a)
  11. }
  12. func (ep *API_EndPoint) EndPoint_X(params string) string {
  13. return "this_is_a_string " + params
  14. }

https://play.golang.org/p/MtqCrshTcH

this_is_a_string foo bar

不确定你想要实现什么,但这应该可以工作。

英文:

.Call returns a slice of reflect.Value so to do what you're trying to do you need to do something like:

  1. package main
  2. import (&quot;reflect&quot;)
  3. type API_EndPoint struct {}
  4. func main() {
  5. var ep API_EndPoint
  6. s := reflect.ValueOf(&amp;ep).MethodByName(&quot;EndPoint_X&quot;).Call([]reflect.Value{reflect.ValueOf(`foo bar`)})
  7. v := reflect.ValueOf(s)
  8. i := v.Interface()
  9. a := i.([]reflect.Value)[0].String() // Get the first index of the slice and call the .String() method on it
  10. println(a)
  11. }
  12. func (ep *API_EndPoint) EndPoint_X( params string) string{
  13. return &quot;this_is_a_string &quot; + params
  14. }

https://play.golang.org/p/MtqCrshTcH

> this_is_a_string foo bar

Not sure what you're trying to accomplish but that should work.

huangapple
  • 本文由 发表于 2016年1月6日 18:28:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/34630833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定