mirror of
https://github.com/golang/go
synced 2024-11-25 06:57:58 -07:00
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
This commit is contained in:
parent
7a3c6c950b
commit
26dc17ce78
@ -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
|
type and value pair stored inside an interface variable. To get
|
||||||
started, there are two types we need to know about in
|
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">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
|
<a href="http://golang.org/pkg/reflect/#Value">Value</a>. Those two types
|
||||||
give access to the contents of an interface variable, and two
|
give access to the contents of an interface variable, and two
|
||||||
simple functions, called <code>reflect.TypeOf</code> and
|
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
|
as in
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre><!--{{code "progs/interface2.go" `/START f3/` `/START/`}}
|
<pre><!--{{code "progs/interface2.go" `/START f3/` `/STOP/`}}
|
||||||
--> type MyInt int
|
--> type MyInt int
|
||||||
var x MyInt = 7
|
var x MyInt = 7
|
||||||
v := reflect.ValueOf(x)</pre>
|
v := reflect.ValueOf(x)</pre>
|
||||||
@ -395,7 +395,7 @@ func (v Value) Interface() interface{}
|
|||||||
As a consequence we can say
|
As a consequence we can say
|
||||||
</p>
|
</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.
|
--> y := v.Interface().(float64) // y will have type float64.
|
||||||
fmt.Println(y)</pre>
|
fmt.Println(y)</pre>
|
||||||
|
|
||||||
@ -415,7 +415,7 @@ the <code>Interface</code> method to the formatted print
|
|||||||
routine:
|
routine:
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre><!--{{code "progs/interface2.go" `/START f3c/` `/START/`}}
|
<pre><!--{{code "progs/interface2.go" `/START f3c/` `/STOP/`}}
|
||||||
--> fmt.Println(v.Interface())</pre>
|
--> fmt.Println(v.Interface())</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@ -518,7 +518,7 @@ determined by whether the reflection object holds the original
|
|||||||
item. When we say
|
item. When we say
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre><!--{{code "progs/interface2.go" `/START f6/` `/START/`}}
|
<pre><!--{{code "progs/interface2.go" `/START f6/` `/STOP/`}}
|
||||||
--> var x float64 = 3.4
|
--> var x float64 = 3.4
|
||||||
v := reflect.ValueOf(x)</pre>
|
v := reflect.ValueOf(x)</pre>
|
||||||
|
|
||||||
@ -577,7 +577,7 @@ and then create a reflection value that points to it, called
|
|||||||
<code>p</code>.
|
<code>p</code>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre><!--{{code "progs/interface2.go" `/START f7/` `/START/`}}
|
<pre><!--{{code "progs/interface2.go" `/START f7/` `/STOP/`}}
|
||||||
--> var x float64 = 3.4
|
--> var x float64 = 3.4
|
||||||
p := reflect.ValueOf(&x) // Note: take the address of x.
|
p := reflect.ValueOf(&x) // Note: take the address of x.
|
||||||
fmt.Println("type of p:", p.Type())
|
fmt.Println("type of p:", p.Type())
|
||||||
@ -601,7 +601,7 @@ and save the result in a reflection <code>Value</code> called
|
|||||||
<code>v</code>:
|
<code>v</code>:
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre><!--{{code "progs/interface2.go" `/START f7b/` `/START/`}}
|
<pre><!--{{code "progs/interface2.go" `/START f7b/` `/STOP/`}}
|
||||||
--> v := p.Elem()
|
--> v := p.Elem()
|
||||||
fmt.Println("settability of v:", v.CanSet())</pre>
|
fmt.Println("settability of v:", v.CanSet())</pre>
|
||||||
|
|
||||||
@ -676,10 +676,7 @@ objects.
|
|||||||
f := s.Field(i)
|
f := s.Field(i)
|
||||||
fmt.Printf("%d: %s %s = %v\n", i,
|
fmt.Printf("%d: %s %s = %v\n", i,
|
||||||
typeOfT.Field(i).Name, f.Type(), f.Interface())
|
typeOfT.Field(i).Name, f.Type(), f.Interface())
|
||||||
}
|
}</pre>
|
||||||
s.Field(0).SetInt(77)
|
|
||||||
s.Field(1).SetString("Sunset Strip")
|
|
||||||
fmt.Println("t is now", t)</pre>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
The output of this program is
|
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
|
type and value pair stored inside an interface variable. To get
|
||||||
started, there are two types we need to know about in
|
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">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
|
<a href="http://golang.org/pkg/reflect/#Value">Value</a>. Those two types
|
||||||
give access to the contents of an interface variable, and two
|
give access to the contents of an interface variable, and two
|
||||||
simple functions, called <code>reflect.TypeOf</code> and
|
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
|
as in
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
{{code "progs/interface2.go" `/START f3/` `/START/`}}
|
{{code "progs/interface2.go" `/START f3/` `/STOP/`}}
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
the <code>Kind</code> of <code>v</code> is still
|
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
|
As a consequence we can say
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
{{code "progs/interface2.go" `/START f3b/` `/START/`}}
|
{{code "progs/interface2.go" `/START f3b/` `/STOP/`}}
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
to print the <code>float64</code> value represented by the
|
to print the <code>float64</code> value represented by the
|
||||||
@ -355,7 +355,7 @@ the <code>Interface</code> method to the formatted print
|
|||||||
routine:
|
routine:
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
{{code "progs/interface2.go" `/START f3c/` `/START/`}}
|
{{code "progs/interface2.go" `/START f3c/` `/STOP/`}}
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
(Why not <code>fmt.Println(v)</code>? Because <code>v</code> is a
|
(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
|
item. When we say
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
{{code "progs/interface2.go" `/START f6/` `/START/`}}
|
{{code "progs/interface2.go" `/START f6/` `/STOP/`}}
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
we pass a <em>copy</em> of <code>x</code> to
|
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>.
|
<code>p</code>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
{{code "progs/interface2.go" `/START f7/` `/START/`}}
|
{{code "progs/interface2.go" `/START f7/` `/STOP/`}}
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
The output so far is
|
The output so far is
|
||||||
@ -526,7 +526,7 @@ and save the result in a reflection <code>Value</code> called
|
|||||||
<code>v</code>:
|
<code>v</code>:
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
{{code "progs/interface2.go" `/START f7b/` `/START/`}}
|
{{code "progs/interface2.go" `/START f7b/` `/STOP/`}}
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Now <code>v</code> is a settable reflection object, as the output
|
Now <code>v</code> is a settable reflection object, as the output
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
|
// 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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
|
// 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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -39,11 +45,14 @@ func f3() {
|
|||||||
type MyInt int
|
type MyInt int
|
||||||
var x MyInt = 7
|
var x MyInt = 7
|
||||||
v := reflect.ValueOf(x)
|
v := reflect.ValueOf(x)
|
||||||
|
// STOP OMIT
|
||||||
// START f3b OMIT
|
// START f3b OMIT
|
||||||
y := v.Interface().(float64) // y will have type float64.
|
y := v.Interface().(float64) // y will have type float64.
|
||||||
fmt.Println(y)
|
fmt.Println(y)
|
||||||
|
// STOP OMIT
|
||||||
// START f3c OMIT
|
// START f3c OMIT
|
||||||
fmt.Println(v.Interface())
|
fmt.Println(v.Interface())
|
||||||
|
// STOP OMIT
|
||||||
// START f3d OMIT
|
// START f3d OMIT
|
||||||
fmt.Printf("value is %7.1e\n", v.Interface())
|
fmt.Printf("value is %7.1e\n", v.Interface())
|
||||||
// STOP OMIT
|
// STOP OMIT
|
||||||
@ -69,6 +78,7 @@ func f6() {
|
|||||||
// START f6 OMIT
|
// START f6 OMIT
|
||||||
var x float64 = 3.4
|
var x float64 = 3.4
|
||||||
v := reflect.ValueOf(x)
|
v := reflect.ValueOf(x)
|
||||||
|
// STOP OMIT
|
||||||
// START f6b OMIT
|
// START f6b OMIT
|
||||||
v.SetFloat(7.1)
|
v.SetFloat(7.1)
|
||||||
// STOP OMIT
|
// STOP OMIT
|
||||||
@ -80,9 +90,11 @@ func f7() {
|
|||||||
p := reflect.ValueOf(&x) // Note: take the address of x.
|
p := reflect.ValueOf(&x) // Note: take the address of x.
|
||||||
fmt.Println("type of p:", p.Type())
|
fmt.Println("type of p:", p.Type())
|
||||||
fmt.Println("settability of p:", p.CanSet())
|
fmt.Println("settability of p:", p.CanSet())
|
||||||
|
// STOP OMIT
|
||||||
// START f7b OMIT
|
// START f7b OMIT
|
||||||
v := p.Elem()
|
v := p.Elem()
|
||||||
fmt.Println("settability of v:", v.CanSet())
|
fmt.Println("settability of v:", v.CanSet())
|
||||||
|
// STOP OMIT
|
||||||
// START f7c OMIT
|
// START f7c OMIT
|
||||||
v.SetFloat(7.1)
|
v.SetFloat(7.1)
|
||||||
fmt.Println(v.Interface())
|
fmt.Println(v.Interface())
|
||||||
@ -104,6 +116,7 @@ func f8() {
|
|||||||
fmt.Printf("%d: %s %s = %v\n", i,
|
fmt.Printf("%d: %s %s = %v\n", i,
|
||||||
typeOfT.Field(i).Name, f.Type(), f.Interface())
|
typeOfT.Field(i).Name, f.Type(), f.Interface())
|
||||||
}
|
}
|
||||||
|
// STOP OMIT
|
||||||
// START f8b OMIT
|
// START f8b OMIT
|
||||||
s.Field(0).SetInt(77)
|
s.Field(0).SetInt(77)
|
||||||
s.Field(1).SetString("Sunset Strip")
|
s.Field(1).SetString("Sunset Strip")
|
||||||
|
Loading…
Reference in New Issue
Block a user