Shenghou Ma | 5b7562d | 2012-09-03 03:49:03 +0800 | [diff] [blame] | 1 | // cmpout |
| 2 | |
Johan Euphrosine | 26dc17c | 2012-03-07 11:24:00 +1100 | [diff] [blame] | 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 | |
Johan Euphrosine | 6652b0b | 2012-03-01 10:05:51 +1100 | [diff] [blame] | 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) |
Johan Euphrosine | 26dc17c | 2012-03-07 11:24:00 +1100 | [diff] [blame] | 50 | // STOP OMIT |
Johan Euphrosine | 6652b0b | 2012-03-01 10:05:51 +1100 | [diff] [blame] | 51 | // START f3b OMIT |
| 52 | y := v.Interface().(float64) // y will have type float64. |
| 53 | fmt.Println(y) |
Johan Euphrosine | 26dc17c | 2012-03-07 11:24:00 +1100 | [diff] [blame] | 54 | // STOP OMIT |
Johan Euphrosine | 6652b0b | 2012-03-01 10:05:51 +1100 | [diff] [blame] | 55 | // START f3c OMIT |
| 56 | fmt.Println(v.Interface()) |
Johan Euphrosine | 26dc17c | 2012-03-07 11:24:00 +1100 | [diff] [blame] | 57 | // STOP OMIT |
Johan Euphrosine | 6652b0b | 2012-03-01 10:05:51 +1100 | [diff] [blame] | 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) |
Johan Euphrosine | 26dc17c | 2012-03-07 11:24:00 +1100 | [diff] [blame] | 83 | // STOP OMIT |
Johan Euphrosine | 6652b0b | 2012-03-01 10:05:51 +1100 | [diff] [blame] | 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()) |
Johan Euphrosine | 26dc17c | 2012-03-07 11:24:00 +1100 | [diff] [blame] | 95 | // STOP OMIT |
Johan Euphrosine | 6652b0b | 2012-03-01 10:05:51 +1100 | [diff] [blame] | 96 | // START f7b OMIT |
| 97 | v := p.Elem() |
| 98 | fmt.Println("settability of v:", v.CanSet()) |
Johan Euphrosine | 26dc17c | 2012-03-07 11:24:00 +1100 | [diff] [blame] | 99 | // STOP OMIT |
Johan Euphrosine | 6652b0b | 2012-03-01 10:05:51 +1100 | [diff] [blame] | 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 | } |
Johan Euphrosine | 26dc17c | 2012-03-07 11:24:00 +1100 | [diff] [blame] | 121 | // STOP OMIT |
Johan Euphrosine | 6652b0b | 2012-03-01 10:05:51 +1100 | [diff] [blame] | 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 | } |
Francisco Souza | 2b3d6cb | 2012-03-21 16:42:04 -0700 | [diff] [blame] | 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 | } |