No, I don't think this is a duplicate of How to determine an interface{} value's "real" type?. I know how to get the Type of an interface variable, but I can't find a way to get the pointer to an interface{}'s real type.
interface{}interface{}Tt
Ttinterface{}
reflect.TypeOf(v)Areflect.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:{
dosomething for []byte
}
default:{
var m reflect.Value
if value.Kind() != reflect.Ptr {
m = reflect.ValueOf(&v).MethodByName("SomeMethod")
} else {
m = reflect.ValueOf(v).MethodByName("SomeMethod")
}
m.Call(nil)
}
}
func main() {
DoSomething([]byte{...})
DoSomething(SomeStruct1{})
DoSomething(&SomeStruct1{})
etc..
}