blob: 85e7d51636494e13542612b8109b032daf940357 [file] [log] [blame]
Shenghou Ma5b7562d2012-09-03 03:49:03 +08001// cmpout
2
Johan Euphrosine26dc17c2012-03-07 11:24:00 +11003// 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 Euphrosine6652b0b2012-03-01 10:05:51 +11009package main
10
11import (
12 "fmt"
13 "reflect"
14)
15
16func 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
25func 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
35func 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
45func f3() {
46 // START f3 OMIT
47 type MyInt int
48 var x MyInt = 7
49 v := reflect.ValueOf(x)
Johan Euphrosine26dc17c2012-03-07 11:24:00 +110050 // STOP OMIT
Johan Euphrosine6652b0b2012-03-01 10:05:51 +110051 // START f3b OMIT
52 y := v.Interface().(float64) // y will have type float64.
53 fmt.Println(y)
Johan Euphrosine26dc17c2012-03-07 11:24:00 +110054 // STOP OMIT
Johan Euphrosine6652b0b2012-03-01 10:05:51 +110055 // START f3c OMIT
56 fmt.Println(v.Interface())
Johan Euphrosine26dc17c2012-03-07 11:24:00 +110057 // STOP OMIT
Johan Euphrosine6652b0b2012-03-01 10:05:51 +110058 // START f3d OMIT
59 fmt.Printf("value is %7.1e\n", v.Interface())
60 // STOP OMIT
61}
62
63func 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
71func 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
79func f6() {
80 // START f6 OMIT
81 var x float64 = 3.4
82 v := reflect.ValueOf(x)
Johan Euphrosine26dc17c2012-03-07 11:24:00 +110083 // STOP OMIT
Johan Euphrosine6652b0b2012-03-01 10:05:51 +110084 // START f6b OMIT
85 v.SetFloat(7.1)
86 // STOP OMIT
87}
88
89func 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 Euphrosine26dc17c2012-03-07 11:24:00 +110095 // STOP OMIT
Johan Euphrosine6652b0b2012-03-01 10:05:51 +110096 // START f7b OMIT
97 v := p.Elem()
98 fmt.Println("settability of v:", v.CanSet())
Johan Euphrosine26dc17c2012-03-07 11:24:00 +110099 // STOP OMIT
Johan Euphrosine6652b0b2012-03-01 10:05:51 +1100100 // START f7c OMIT
101 v.SetFloat(7.1)
102 fmt.Println(v.Interface())
103 fmt.Println(x)
104 // STOP OMIT
105}
106
107func 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 Euphrosine26dc17c2012-03-07 11:24:00 +1100121 // STOP OMIT
Johan Euphrosine6652b0b2012-03-01 10:05:51 +1100122 // 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 Souza2b3d6cb2012-03-21 16:42:04 -0700128
129func f9() {
130 // START f9 OMIT
131 var x float64 = 3.4
132 fmt.Println("value:", reflect.ValueOf(x))
133 // STOP OMIT
134}