The long-form (with comma-ok) ones were already fixed but the short-form ones were not because they were in a completely different code path. This PR also refactors the code so that both short-form and long-form are now merged in the same function. N.B: even though most (all?) cases seem to now be supported, one of them still yields a result that does not satisfy reflect's Implements method yet. It does not prevent the resulting assertion to be usable though. N.B2: the code path for the third-form (_, ok) hasn't been fixed and/or refactored yet. Fixes #919
97 lines
1.8 KiB
Go
97 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"time"
|
|
)
|
|
|
|
type MyWriter interface {
|
|
Write(p []byte) (i int, err error)
|
|
}
|
|
|
|
type TestStruct struct{}
|
|
|
|
func (t TestStruct) Write(p []byte) (n int, err error) {
|
|
return len(p), nil
|
|
}
|
|
|
|
func usesWriter(w MyWriter) {
|
|
n, _ := w.Write([]byte("hello world"))
|
|
fmt.Println(n)
|
|
}
|
|
|
|
type MyStringer interface {
|
|
String() string
|
|
}
|
|
|
|
func usesStringer(s MyStringer) {
|
|
fmt.Println(s.String())
|
|
}
|
|
|
|
func main() {
|
|
aType := reflect.TypeOf((*MyWriter)(nil)).Elem()
|
|
|
|
var t interface{}
|
|
t = TestStruct{}
|
|
var tw MyWriter
|
|
var ok bool
|
|
tw, ok = t.(MyWriter)
|
|
if !ok {
|
|
fmt.Println("TestStruct does not implement MyWriter")
|
|
} else {
|
|
fmt.Println("TestStruct implements MyWriter")
|
|
usesWriter(tw)
|
|
}
|
|
n, _ := t.(MyWriter).Write([]byte("hello world"))
|
|
fmt.Println(n)
|
|
bType := reflect.TypeOf(TestStruct{})
|
|
fmt.Println(bType.Implements(aType))
|
|
|
|
t = 42
|
|
foo, ok := t.(MyWriter)
|
|
if !ok {
|
|
fmt.Println("42 does not implement MyWriter")
|
|
} else {
|
|
fmt.Println("42 implements MyWriter")
|
|
}
|
|
_ = foo
|
|
|
|
var tt interface{}
|
|
tt = time.Nanosecond
|
|
var myD MyStringer
|
|
myD, ok = tt.(MyStringer)
|
|
if !ok {
|
|
fmt.Println("time.Nanosecond does not implement MyStringer")
|
|
} else {
|
|
fmt.Println("time.Nanosecond implements MyStringer")
|
|
usesStringer(myD)
|
|
}
|
|
fmt.Println(tt.(MyStringer).String())
|
|
cType := reflect.TypeOf((*MyStringer)(nil)).Elem()
|
|
dType := reflect.TypeOf(time.Nanosecond)
|
|
fmt.Println(dType.Implements(cType))
|
|
|
|
tt = 42
|
|
bar, ok := tt.(MyStringer)
|
|
if !ok {
|
|
fmt.Println("42 does not implement MyStringer")
|
|
} else {
|
|
fmt.Println("42 implements MyStringer")
|
|
}
|
|
_ = bar
|
|
|
|
}
|
|
|
|
// Output:
|
|
// TestStruct implements MyWriter
|
|
// 11
|
|
// 11
|
|
// true
|
|
// 42 does not implement MyWriter
|
|
// time.Nanosecond implements MyStringer
|
|
// 1ns
|
|
// 1ns
|
|
// true
|
|
// 42 does not implement MyStringer
|