Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
14da5298
Commit
14da5298
authored
Mar 27, 2012
by
Andrew Gerrand
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
doc: use relative links in Laws of Reflection article
R=golang-dev, r CC=golang-dev
https://golang.org/cl/5924050
parent
cafc2b6a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
26 deletions
+21
-26
laws_of_reflection.html
doc/articles/laws_of_reflection.html
+21
-26
No files found.
doc/articles/laws_of_reflection.html
View file @
14da5298
...
...
@@ -48,8 +48,8 @@ fixed sets of methods. An interface variable can store any concrete
(non-interface) value as long as that value implements the
interface's methods. A well-known pair of examples is
<code>
io.Reader
</code>
and
<code>
io.Writer
</code>
, the types
<code>
Reader
</code>
and
<code>
Writer
</code>
from the
<a
href=
"http://golang.org
/pkg/io/"
>
io package
</a>
:
<code>
Reader
</code>
and
<code>
Writer
</code>
from the
<a
href=
"
/pkg/io/"
>
io package
</a>
:
</p>
{{code "/doc/progs/interface.go" `/// Reader/` `/STOP/`}}
...
...
@@ -101,11 +101,10 @@ interfaces are closely related.
<p><b>
The representation of an interface
</b></p>
<p>
Russ Cox has written a
<a
href=
"http://research.swtch.com/2009/12/go-data-structures-interfaces.html"
>
detailed blog post
</a>
about the representation of interface values
in Go. It's not necessary to repeat the full story here, but a
simplified summary is in order.
Russ Cox has written a
<a
href=
"http://research.swtch.com/2009/12/go-data-structures-interfaces.html"
>
detailed blog post
</a>
about the representation of interface values in Go. It's not necessary to
repeat the full story here, but a simplified summary is in order.
</p>
<p>
...
...
@@ -183,9 +182,9 @@ Now we're ready to reflect.
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/#Value"
>
Value
</a>
. Those two types
<a
href=
"
/pkg/reflect/
"
>
package reflect
</a>
:
<a
href=
"/pkg/reflect/#Type"
>
Type
</a>
and
<a
href=
"/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
<code>
reflect.ValueOf
</code>
, retrieve
<code>
reflect.Type
</code>
...
...
@@ -211,13 +210,11 @@ type: float64
</pre>
<p>
You might be wondering where the interface is here, since the
program looks like it's passing the
<code>
float64
</code>
variable
<code>
x
</code>
, not an interface value, to
<code>
reflect.TypeOf
</code>
. But it's there; as
<a
href=
"http://golang.org/pkg/reflect/#Type.TypeOf"
>
godoc reports
</a>
, the
signature of
<code>
reflect.TypeOf
</code>
includes an empty
interface:
You might be wondering where the interface is here, since the program looks
like it's passing the
<code>
float64
</code>
variable
<code>
x
</code>
, not an
interface value, to
<code>
reflect.TypeOf
</code>
. But it's there; as
<a
href=
"/pkg/reflect/#Type.TypeOf"
>
godoc reports
</a>
, the signature of
<code>
reflect.TypeOf
</code>
includes an empty interface:
</p>
<pre>
...
...
@@ -573,15 +570,13 @@ fields.
</p>
<p>
Here's a simple example that analyzes a struct value,
<code>
t
</code>
. We create the reflection object with the address of
the struct because we'll want to modify it later. Then we set
<code>
typeOfT
</code>
to its type and iterate over the fields using
straightforward method calls (see
<a
href=
"http://golang.org/pkg/reflect/"
>
package reflect
</a>
for details).
Note that we extract the names of the fields from the struct type,
but the fields themselves are regular
<code>
reflect.Value
</code>
objects.
Here's a simple example that analyzes a struct value,
<code>
t
</code>
. We create
the reflection object with the address of the struct because we'll want to
modify it later. Then we set
<code>
typeOfT
</code>
to its type and iterate over
the fields using straightforward method calls
(see
<a
href=
"/pkg/reflect/"
>
package reflect
</a>
for details).
Note that we extract the names of the fields from the struct type, but the
fields themselves are regular
<code>
reflect.Value
</code>
objects.
</p>
{{code "/doc/progs/interface2.go" `/START f8/` `/STOP/`}}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment