不,我不认为这与 如何确定接口{}值的“真实”类型?。我知道如何获取接口变量的类型,但我找不到获取指向 interface{} 真实类型的指针的方法。
最近,我遇到了麻烦interface{}。我有一个类型为 A 的变量被传递interface{},一个方法Tt被定义为 *A 作为接收者。
我想调用该方法Tt但失败了,因为该变量位于 an 中interface{},并且我无法获取指向该变量的指针。
正如您所看到的,reflect.TypeOf(v)给出了正确的类型A,但reflect.TypeOf(&v)给出的*interface {}是*A。
有什么办法可以得到吗*A?
package main
import (
"fmt"
"reflect"
)
type SomeStruct1 struct{
}
type SomeStruct2 struct{
}
type SomeStruct3 struct{
}
etc...
func (*SomeStruct1) SomeMethod(){
fmt.Println("hello")
}
func (*SomeStruct2) SomeMethod(){
fmt.Println("hello")
}
func (*SomeStruct3) SomeMethod(){
fmt.Println("hello")
}
etc...
func DoSomething(arg interface{}){
switch v:=b.(type){
[]byte:{ …Run Code Online (Sandbox Code Playgroud)