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

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

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

问题

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

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

package main

import (
	"reflect"
)

type API_EndPoint struct{}

func main() {

	var ep API_EndPoint
	s := reflect.ValueOf(&ep).MethodByName("EndPoint_X").Call([]reflect.Value{reflect.ValueOf(`foo bar`)})

	v := reflect.ValueOf(s)
	i := v.Interface()
	a := i.(string)

	println(a)

}

func (ep *API_EndPoint) EndPoint_X(params string) string {

	return "this_is_a_string" + params

}

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

英文:

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

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

package main

import (
        "reflect"
)

type API_EndPoint struct{}

func main() {

        var ep API_EndPoint
        s := reflect.ValueOf(&ep).MethodByName("EndPoint_X").Call([]reflect.Value{reflect.ValueOf(`foo bar`)})

        v := reflect.ValueOf(s)
        i := v.Interface()
        a := i.(string)

        println(a)

}

func (ep *API_EndPoint) EndPoint_X(params string) string {

        return "this_is_a_string" + params

}

<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的切片,所以为了实现你想要的效果,你需要像这样做:

package main

import "reflect"

type API_EndPoint struct {}

func main() {

    var ep API_EndPoint
    s := reflect.ValueOf(&ep).MethodByName("EndPoint_X").Call([]reflect.Value{reflect.ValueOf("foo bar")})
     
    v := reflect.ValueOf(s)
    i := v.Interface()
    a := i.([]reflect.Value)[0].String() // 获取切片的第一个索引,并调用它的.String()方法
    
    println(a) 
    
}

func (ep *API_EndPoint) EndPoint_X(params string) string {

    return "this_is_a_string " + params

}

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:

package main

import (&quot;reflect&quot;)

type API_EndPoint struct {}

func main() {

var ep API_EndPoint
s := reflect.ValueOf(&amp;ep).MethodByName(&quot;EndPoint_X&quot;).Call([]reflect.Value{reflect.ValueOf(`foo bar`)})
 
v := reflect.ValueOf(s)
i := v.Interface()
a := i.([]reflect.Value)[0].String() // Get the first index of the slice and call the .String() method on it

println(a) 

}

func (ep *API_EndPoint) EndPoint_X( params string) string{

        return &quot;this_is_a_string &quot; + params

}

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:

确定