vendredi 20 janvier 2023

Go (Golang) reflect Kind is never interface? [duplicate]

Assume we have a struct implementing an interface:

type SomeStruct struct {}
type SomeInterface interface { SomeMethod() }

func (s *SomeStruct) SomeMethod() {}

and some instantiations:

var (
  asValue := SomeStruct{}
  asPointer := &asValue
  asInterface SomeInterface = asPointer
)

Now, using the reflect package, we can see that

reflect.TypeOf(asValue).Kind() == reflect.Struct
reflect.TypeOf(asPointer).Kind() == reflect.Pointer
reflect.TypeOf(asInterface).Kind() == reflect.Pointer // This!
                                                      // Why not reflect.Interface?

So, my question is: what should x be so that we get reflect.TypeOf(x) == reflect.Interface?





Aucun commentaire:

Enregistrer un commentaire