I found that
1. If a function returns interface{}
2. Try to invoke it through reflect.Value.Call()
3. The type of return value will be a pointer
For example: (https://play.golang.org/p/gyimxUzUVYf)
package main
import (
"fmt"
"reflect"
)
type MyStruct struct{}
func (my *MyStruct) Fn() interface{} {
s := "aaa"
return s
}
func main() {
my := &MyStruct{}
{
m := reflect.ValueOf(my).MethodByName("Fn")
fromReflect := m.Call([]reflect.Value{})
fmt.Printf("fromReflect[0].Type() = [%v] \n", fromReflect[0].Type())
fmt.Printf("fromReflect[0].Elem().Type() = [%v] \n",
fromReflect[0].Elem().Type())
}
{
s := my.Fn()
fromFunction := reflect.ValueOf(s)
fmt.Printf("fromFunction.Type() = [%v] \n", fromFunction.Type())
}
}
the result is:
fromReflect[0].Type() = [interface {}]
fromReflect[0].Elem().Type() = [string]
fromFunction.Type() = [string]
I wonder why the types are different?
I expect the result should be:
fromReflect[0].Type() = [string]
Is this a normal behavior for reflect.Value.Call() ?
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.