type and value pair stored inside an interface variable. To get
started, there are two types we need to know about in
<a href="http://golang.org/pkg/reflect">package reflect</a>:
-<a href="http://golang.org/pkg/reflect/#Type">Type</a>and
+<a href="http://golang.org/pkg/reflect/#Type">Type</a> and
<a href="http://golang.org/pkg/reflect/#Value">Value</a>. Those two types
give access to the contents of an interface variable, and two
simple functions, called <code>reflect.TypeOf</code> and
as in
</p>
-<pre><!--{{code "progs/interface2.go" `/START f3/` `/START/`}}
+<pre><!--{{code "progs/interface2.go" `/START f3/` `/STOP/`}}
--> type MyInt int
var x MyInt = 7
v := reflect.ValueOf(x)</pre>
As a consequence we can say
</p>
-<pre><!--{{code "progs/interface2.go" `/START f3b/` `/START/`}}
+<pre><!--{{code "progs/interface2.go" `/START f3b/` `/STOP/`}}
--> y := v.Interface().(float64) // y will have type float64.
fmt.Println(y)</pre>
routine:
</p>
-<pre><!--{{code "progs/interface2.go" `/START f3c/` `/START/`}}
+<pre><!--{{code "progs/interface2.go" `/START f3c/` `/STOP/`}}
--> fmt.Println(v.Interface())</pre>
<p>
item. When we say
</p>
-<pre><!--{{code "progs/interface2.go" `/START f6/` `/START/`}}
+<pre><!--{{code "progs/interface2.go" `/START f6/` `/STOP/`}}
--> var x float64 = 3.4
v := reflect.ValueOf(x)</pre>
<code>p</code>.
</p>
-<pre><!--{{code "progs/interface2.go" `/START f7/` `/START/`}}
+<pre><!--{{code "progs/interface2.go" `/START f7/` `/STOP/`}}
--> var x float64 = 3.4
p := reflect.ValueOf(&x) // Note: take the address of x.
fmt.Println("type of p:", p.Type())
<code>v</code>:
</p>
-<pre><!--{{code "progs/interface2.go" `/START f7b/` `/START/`}}
+<pre><!--{{code "progs/interface2.go" `/START f7b/` `/STOP/`}}
--> v := p.Elem()
fmt.Println("settability of v:", v.CanSet())</pre>
f := s.Field(i)
fmt.Printf("%d: %s %s = %v\n", i,
typeOfT.Field(i).Name, f.Type(), f.Interface())
- }
- s.Field(0).SetInt(77)
- s.Field(1).SetString("Sunset Strip")
- fmt.Println("t is now", t)</pre>
+ }</pre>
<p>
The output of this program is
type and value pair stored inside an interface variable. To get
started, there are two types we need to know about in
<a href="http://golang.org/pkg/reflect">package reflect</a>:
-<a href="http://golang.org/pkg/reflect/#Type">Type</a>and
+<a href="http://golang.org/pkg/reflect/#Type">Type</a> and
<a href="http://golang.org/pkg/reflect/#Value">Value</a>. Those two types
give access to the contents of an interface variable, and two
simple functions, called <code>reflect.TypeOf</code> and
as in
</p>
-{{code "progs/interface2.go" `/START f3/` `/START/`}}
+{{code "progs/interface2.go" `/START f3/` `/STOP/`}}
<p>
the <code>Kind</code> of <code>v</code> is still
As a consequence we can say
</p>
-{{code "progs/interface2.go" `/START f3b/` `/START/`}}
+{{code "progs/interface2.go" `/START f3b/` `/STOP/`}}
<p>
to print the <code>float64</code> value represented by the
routine:
</p>
-{{code "progs/interface2.go" `/START f3c/` `/START/`}}
+{{code "progs/interface2.go" `/START f3c/` `/STOP/`}}
<p>
(Why not <code>fmt.Println(v)</code>? Because <code>v</code> is a
item. When we say
</p>
-{{code "progs/interface2.go" `/START f6/` `/START/`}}
+{{code "progs/interface2.go" `/START f6/` `/STOP/`}}
<p>
we pass a <em>copy</em> of <code>x</code> to
<code>p</code>.
</p>
-{{code "progs/interface2.go" `/START f7/` `/START/`}}
+{{code "progs/interface2.go" `/START f7/` `/STOP/`}}
<p>
The output so far is
<code>v</code>:
</p>
-{{code "progs/interface2.go" `/START f7b/` `/START/`}}
+{{code "progs/interface2.go" `/START f7b/` `/STOP/`}}
<p>
Now <code>v</code> is a settable reflection object, as the output
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// This file contains the code snippets included in "The Laws of Reflection."
+
package main
import (
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// This file contains the code snippets included in "The Laws of Reflection."
+
package main
import (
type MyInt int
var x MyInt = 7
v := reflect.ValueOf(x)
+ // STOP OMIT
// START f3b OMIT
y := v.Interface().(float64) // y will have type float64.
fmt.Println(y)
+ // STOP OMIT
// START f3c OMIT
fmt.Println(v.Interface())
+ // STOP OMIT
// START f3d OMIT
fmt.Printf("value is %7.1e\n", v.Interface())
// STOP OMIT
// START f6 OMIT
var x float64 = 3.4
v := reflect.ValueOf(x)
+ // STOP OMIT
// START f6b OMIT
v.SetFloat(7.1)
// STOP OMIT
p := reflect.ValueOf(&x) // Note: take the address of x.
fmt.Println("type of p:", p.Type())
fmt.Println("settability of p:", p.CanSet())
+ // STOP OMIT
// START f7b OMIT
v := p.Elem()
fmt.Println("settability of v:", v.CanSet())
+ // STOP OMIT
// START f7c OMIT
v.SetFloat(7.1)
fmt.Println(v.Interface())
fmt.Printf("%d: %s %s = %v\n", i,
typeOfT.Field(i).Name, f.Type(), f.Interface())
}
+ // STOP OMIT
// START f8b OMIT
s.Field(0).SetInt(77)
s.Field(1).SetString("Sunset Strip")