Commit 26dc17ce authored by Johan Euphrosine's avatar Johan Euphrosine Committed by Andrew Gerrand

doc: fix typos in laws_of_reflection article, add copyright notice.

Update #2547.

R=golang-dev, minux.ma, r, r, adg
CC=golang-dev
https://golang.org/cl/5755051
parent 7a3c6c95
......@@ -216,7 +216,7 @@ At the basic level, reflection is just a mechanism to examine the
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
......@@ -356,7 +356,7 @@ reflection object contains a value of a user-defined integer type,
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>
......@@ -395,7 +395,7 @@ func (v Value) Interface() interface{}
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>
......@@ -415,7 +415,7 @@ the <code>Interface</code> method to the formatted print
routine:
</p>
<pre><!--{{code "progs/interface2.go" `/START f3c/` `/START/`}}
<pre><!--{{code "progs/interface2.go" `/START f3c/` `/STOP/`}}
--> fmt.Println(v.Interface())</pre>
<p>
......@@ -518,7 +518,7 @@ determined by whether the reflection object holds the original
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>
......@@ -577,7 +577,7 @@ and then create a reflection value that points to it, called
<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(&amp;x) // Note: take the address of x.
fmt.Println(&#34;type of p:&#34;, p.Type())
......@@ -601,7 +601,7 @@ and save the result in a reflection <code>Value</code> called
<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(&#34;settability of v:&#34;, v.CanSet())</pre>
......@@ -676,10 +676,7 @@ objects.
f := s.Field(i)
fmt.Printf(&#34;%d: %s %s = %v\n&#34;, i,
typeOfT.Field(i).Name, f.Type(), f.Interface())
}
s.Field(0).SetInt(77)
s.Field(1).SetString(&#34;Sunset Strip&#34;)
fmt.Println(&#34;t is now&#34;, t)</pre>
}</pre>
<p>
The output of this program is
......
......@@ -184,7 +184,7 @@ At the basic level, reflection is just a mechanism to examine the
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
......@@ -301,7 +301,7 @@ reflection object contains a value of a user-defined integer type,
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
......@@ -337,7 +337,7 @@ func (v Value) Interface() interface{}
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
......@@ -355,7 +355,7 @@ the <code>Interface</code> method to the formatted print
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
......@@ -450,7 +450,7 @@ determined by whether the reflection object holds the original
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
......@@ -506,7 +506,7 @@ and then create a reflection value that points to it, called
<code>p</code>.
</p>
{{code "progs/interface2.go" `/START f7/` `/START/`}}
{{code "progs/interface2.go" `/START f7/` `/STOP/`}}
<p>
The output so far is
......@@ -526,7 +526,7 @@ and save the result in a reflection <code>Value</code> called
<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 (
......@@ -39,11 +45,14 @@ func f3() {
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
......@@ -69,6 +78,7 @@ func f6() {
// START f6 OMIT
var x float64 = 3.4
v := reflect.ValueOf(x)
// STOP OMIT
// START f6b OMIT
v.SetFloat(7.1)
// STOP OMIT
......@@ -80,9 +90,11 @@ func f7() {
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())
......@@ -104,6 +116,7 @@ func f8() {
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")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment