if t, ok := reflect.TypeOf(x).Elem().(fmt.Stringer); ok { // do something with t, which is now a fmt.Stringer } else { // x does not implement fmt.Stringer }
if _, ok := reflect.TypeOf(x).Elem().(error); ok { // we can treat x as an error } else { // x is not an error }These examples demonstrate how Go's `reflect` package can be used to dynamically check the capabilities of a type at runtime.