diff --git a/src/reflect/example_test.go b/src/reflect/example_test.go index cca28eeece..8ebf9765b8 100644 --- a/src/reflect/example_test.go +++ b/src/reflect/example_test.go @@ -6,6 +6,8 @@ package reflect_test import ( "fmt" + "io" + "os" "reflect" ) @@ -64,3 +66,16 @@ func ExampleStructTag() { // Output: // blue gopher } + +func ExampleTypeOf() { + // As interface types are only used for static typing, a + // common idiom to find the reflection Type for an interface + // type Foo is to use a *Foo value. + writerType := reflect.TypeOf((*io.Writer)(nil)).Elem() + + fileType := reflect.TypeOf((*os.File)(nil)) + fmt.Println(fileType.Implements(writerType)) + + // Output: + // true +} diff --git a/src/reflect/type.go b/src/reflect/type.go index 48d9b85797..3e46ce0aaa 100644 --- a/src/reflect/type.go +++ b/src/reflect/type.go @@ -1009,8 +1009,8 @@ func (t *structType) FieldByName(name string) (f StructField, present bool) { return t.FieldByNameFunc(func(s string) bool { return s == name }) } -// TypeOf returns the reflection Type of the value in the interface{}. -// TypeOf(nil) returns nil. +// TypeOf returns the reflection Type that represents the dynamic type of i. +// If i is a nil interface value, TypeOf returns nil. func TypeOf(i interface{}) Type { eface := *(*emptyInterface)(unsafe.Pointer(&i)) return toType(eface.typ)