]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: add an example for Kind
authorDon Byington <don@dbyington.com>
Wed, 3 Oct 2018 21:54:54 +0000 (21:54 +0000)
committerIan Lance Taylor <iant@golang.org>
Wed, 3 Oct 2018 22:38:53 +0000 (22:38 +0000)
Fixes #27990

Change-Id: I0f09fc6f68cec770b1c26eed2315afbf6bf6cd4d
GitHub-Last-Rev: 8486e6d5019c6c21b10e5fcf10a2727cf2705174
GitHub-Pull-Request: golang/go#27991
Reviewed-on: https://go-review.googlesource.com/c/139417
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/reflect/example_test.go

index f959b95846383d822509a87f0d44deb682be0a38..23c08e4950049ed44f818d1d118ef1892adb1ad9 100644 (file)
@@ -13,6 +13,24 @@ import (
        "reflect"
 )
 
+func ExampleKind() {
+       for _, v := range []interface{}{"hi", 42, func() {}} {
+               switch v := reflect.ValueOf(v); v.Kind() {
+               case reflect.String:
+                       fmt.Println(v.String())
+               case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+                       fmt.Println(v.Int())
+               default:
+                       fmt.Printf("unhandled kind %s", v.Kind())
+               }
+       }
+
+       // Output:
+       // hi
+       // 42
+       // unhandled kind func
+}
+
 func ExampleMakeFunc() {
        // swap is the implementation passed to MakeFunc.
        // It must work in terms of reflect.Values so that it is possible