diff --git a/doc/articles/laws_of_reflection.html b/doc/articles/laws_of_reflection.html index 4df70e0d2cb..37eb96bb6b0 100644 --- a/doc/articles/laws_of_reflection.html +++ b/doc/articles/laws_of_reflection.html @@ -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 package reflect: -Typeand +Type and Value. Those two types give access to the contents of an interface variable, and two simple functions, called reflect.TypeOf and @@ -356,7 +356,7 @@ reflection object contains a value of a user-defined integer type, as in

-
    type MyInt int
     var x MyInt = 7
     v := reflect.ValueOf(x)
@@ -395,7 +395,7 @@ func (v Value) Interface() interface{} As a consequence we can say

-
    y := v.Interface().(float64) // y will have type float64.
     fmt.Println(y)
@@ -415,7 +415,7 @@ the Interface method to the formatted print routine:

-
    fmt.Println(v.Interface())

@@ -518,7 +518,7 @@ determined by whether the reflection object holds the original item. When we say

-
    var x float64 = 3.4
     v := reflect.ValueOf(x)
@@ -577,7 +577,7 @@ and then create a reflection value that points to it, called p.

-
    var x float64 = 3.4
     p := reflect.ValueOf(&x) // Note: take the address of x.
     fmt.Println("type of p:", p.Type())
@@ -601,7 +601,7 @@ and save the result in a reflection Value called
 v:
 

-
    v := p.Elem()
     fmt.Println("settability of v:", v.CanSet())
@@ -676,10 +676,7 @@ objects. 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)
+ }

The output of this program is diff --git a/doc/articles/laws_of_reflection.tmpl b/doc/articles/laws_of_reflection.tmpl index 7db5d6d3b56..d89566f6225 100644 --- a/doc/articles/laws_of_reflection.tmpl +++ b/doc/articles/laws_of_reflection.tmpl @@ -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 package reflect: -Typeand +Type and Value. Those two types give access to the contents of an interface variable, and two simple functions, called reflect.TypeOf and @@ -301,7 +301,7 @@ reflection object contains a value of a user-defined integer type, as in

-{{code "progs/interface2.go" `/START f3/` `/START/`}} +{{code "progs/interface2.go" `/START f3/` `/STOP/`}}

the Kind of v is still @@ -337,7 +337,7 @@ func (v Value) Interface() interface{} As a consequence we can say

-{{code "progs/interface2.go" `/START f3b/` `/START/`}} +{{code "progs/interface2.go" `/START f3b/` `/STOP/`}}

to print the float64 value represented by the @@ -355,7 +355,7 @@ the Interface method to the formatted print routine:

-{{code "progs/interface2.go" `/START f3c/` `/START/`}} +{{code "progs/interface2.go" `/START f3c/` `/STOP/`}}

(Why not fmt.Println(v)? Because v is a @@ -450,7 +450,7 @@ determined by whether the reflection object holds the original item. When we say

-{{code "progs/interface2.go" `/START f6/` `/START/`}} +{{code "progs/interface2.go" `/START f6/` `/STOP/`}}

we pass a copy of x to @@ -506,7 +506,7 @@ and then create a reflection value that points to it, called p.

-{{code "progs/interface2.go" `/START f7/` `/START/`}} +{{code "progs/interface2.go" `/START f7/` `/STOP/`}}

The output so far is @@ -526,7 +526,7 @@ and save the result in a reflection Value called v:

-{{code "progs/interface2.go" `/START f7b/` `/START/`}} +{{code "progs/interface2.go" `/START f7b/` `/STOP/`}}

Now v is a settable reflection object, as the output diff --git a/doc/progs/interface.go b/doc/progs/interface.go index 91145401e22..c2925d590d5 100644 --- a/doc/progs/interface.go +++ b/doc/progs/interface.go @@ -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 import ( diff --git a/doc/progs/interface2.go b/doc/progs/interface2.go index e2716cf16d3..2deba32b464 100644 --- a/doc/progs/interface2.go +++ b/doc/progs/interface2.go @@ -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 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")