]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: document reflect.TypeOf((*Foo)(nil)).Elem() idiom
authorMatthew Dempsky <mdempsky@google.com>
Tue, 14 Apr 2015 16:56:05 +0000 (09:56 -0700)
committerRob Pike <r@golang.org>
Tue, 14 Apr 2015 17:19:36 +0000 (17:19 +0000)
See also golang-dev discussion:
https://groups.google.com/d/msg/golang-dev/Nk9gnTINlTg/SV8rBt-2__kJ

Change-Id: I49edd98d73400c1757b6085dec86752de569c01a
Reviewed-on: https://go-review.googlesource.com/8923
Reviewed-by: Rob Pike <r@golang.org>
src/reflect/example_test.go
src/reflect/type.go

index cca28eeece8c2b994735d33ddcf603a27680ea73..8ebf9765b8dfb29bd075e9d4b6c65bd2634a1179 100644 (file)
@@ -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
+}
index 48d9b85797312892050a692e415b1282c6cd5ec6..3e46ce0aaaf8a9ac201201796b5dc4eed6c72119 100644 (file)
@@ -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)