How to get the reflect.Type of an interface?

go

Solution

Do it like this:

var err error
t := reflect.TypeOf(&err).Elem()

Or in one line:

t := reflect.TypeOf((*error)(nil)).Elem()

Problem

In order to determine whether a given type implements an interface using the reflect package, you need to pass a reflect.Type to reflect.Type.Implements(). How do you get one of those types? As an example, trying to get the type of an uninitialized `error` (interface) type does not work (it panics when you to call Kind() on it) ``` var err error fmt.Printf("%#v\n", reflect.TypeOf(err).Kind()) ```

Original source

Related problems