github.com/hongwozai/go-src-1.4.3@v0.0.0-20191127132709-dc3fce3dbccb/doc/progs/interface2.go (about) 1 // cmpout 2 3 // Copyright 2012 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // This file contains the code snippets included in "The Laws of Reflection." 8 9 package main 10 11 import ( 12 "fmt" 13 "reflect" 14 ) 15 16 func main() { 17 var x float64 = 3.4 18 fmt.Println("type:", reflect.TypeOf(x)) 19 // STOP OMIT 20 // TODO(proppy): test output OMIT 21 } 22 23 // STOP main OMIT 24 25 func f1() { 26 // START f1 OMIT 27 var x float64 = 3.4 28 v := reflect.ValueOf(x) 29 fmt.Println("type:", v.Type()) 30 fmt.Println("kind is float64:", v.Kind() == reflect.Float64) 31 fmt.Println("value:", v.Float()) 32 // STOP OMIT 33 } 34 35 func f2() { 36 // START f2 OMIT 37 var x uint8 = 'x' 38 v := reflect.ValueOf(x) 39 fmt.Println("type:", v.Type()) // uint8. 40 fmt.Println("kind is uint8: ", v.Kind() == reflect.Uint8) // true. 41 x = uint8(v.Uint()) // v.Uint returns a uint64. 42 // STOP OMIT 43 } 44 45 func f3() { 46 // START f3 OMIT 47 type MyInt int 48 var x MyInt = 7 49 v := reflect.ValueOf(x) 50 // STOP OMIT 51 // START f3b OMIT 52 y := v.Interface().(float64) // y will have type float64. 53 fmt.Println(y) 54 // STOP OMIT 55 // START f3c OMIT 56 fmt.Println(v.Interface()) 57 // STOP OMIT 58 // START f3d OMIT 59 fmt.Printf("value is %7.1e\n", v.Interface()) 60 // STOP OMIT 61 } 62 63 func f4() { 64 // START f4 OMIT 65 var x float64 = 3.4 66 v := reflect.ValueOf(x) 67 v.SetFloat(7.1) // Error: will panic. 68 // STOP OMIT 69 } 70 71 func f5() { 72 // START f5 OMIT 73 var x float64 = 3.4 74 v := reflect.ValueOf(x) 75 fmt.Println("settability of v:", v.CanSet()) 76 // STOP OMIT 77 } 78 79 func f6() { 80 // START f6 OMIT 81 var x float64 = 3.4 82 v := reflect.ValueOf(x) 83 // STOP OMIT 84 // START f6b OMIT 85 v.SetFloat(7.1) 86 // STOP OMIT 87 } 88 89 func f7() { 90 // START f7 OMIT 91 var x float64 = 3.4 92 p := reflect.ValueOf(&x) // Note: take the address of x. 93 fmt.Println("type of p:", p.Type()) 94 fmt.Println("settability of p:", p.CanSet()) 95 // STOP OMIT 96 // START f7b OMIT 97 v := p.Elem() 98 fmt.Println("settability of v:", v.CanSet()) 99 // STOP OMIT 100 // START f7c OMIT 101 v.SetFloat(7.1) 102 fmt.Println(v.Interface()) 103 fmt.Println(x) 104 // STOP OMIT 105 } 106 107 func f8() { 108 // START f8 OMIT 109 type T struct { 110 A int 111 B string 112 } 113 t := T{23, "skidoo"} 114 s := reflect.ValueOf(&t).Elem() 115 typeOfT := s.Type() 116 for i := 0; i < s.NumField(); i++ { 117 f := s.Field(i) 118 fmt.Printf("%d: %s %s = %v\n", i, 119 typeOfT.Field(i).Name, f.Type(), f.Interface()) 120 } 121 // STOP OMIT 122 // START f8b OMIT 123 s.Field(0).SetInt(77) 124 s.Field(1).SetString("Sunset Strip") 125 fmt.Println("t is now", t) 126 // STOP OMIT 127 } 128 129 func f9() { 130 // START f9 OMIT 131 var x float64 = 3.4 132 fmt.Println("value:", reflect.ValueOf(x)) 133 // STOP OMIT 134 }