diff options
author | Rob Pike <r@golang.org> | 2017-04-04 12:43:15 -0700 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2017-04-04 19:59:40 +0000 |
commit | b6b47aa8b22a973280c003145c95acf42b68ccb7 (patch) | |
tree | a58743a2366a643313a5041abbc42ff73690d88c | |
parent | c36abd32f2bd6e4a9f96085bdb4e0c534e6fe5fc (diff) |
blog: update one example in "Laws of Reflection"
A recent change to fmt that has it print reflect.Values nicer
invalidates the pedagogical value of one example. Tweak the
example.
Change-Id: I9ca7f81957832b26c47270d8967b7aeef801b1a8
Reviewed-on: https://go-review.googlesource.com/39530
Reviewed-by: Ian Lance Taylor <iant@golang.org>
-rw-r--r-- | content/laws-of-reflection.article | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/content/laws-of-reflection.article b/content/laws-of-reflection.article index a95ac0e..fc704f2 100644 --- a/content/laws-of-reflection.article +++ b/content/laws-of-reflection.article @@ -122,12 +122,15 @@ When we call `reflect.TypeOf(x)`, `x` is first stored in an empty interface, whi The `reflect.ValueOf` function, of course, recovers the value (from here on we'll elide the boilerplate and focus just on the executable code): var x float64 = 3.4 - fmt.Println("value:", reflect.ValueOf(x)) + fmt.Println("value:", reflect.ValueOf(x).String()) prints value: <float64 Value> +(We call the `String` method explicitly because by default the `fmt` package digs into a `reflect.Value` to show the concrete value inside. +The `String` method does not.) + Both `reflect.Type` and `reflect.Value` have lots of methods to let us examine and manipulate them. One important example is that `Value` has a `Type` method that returns the `Type` of a `reflect.Value`. Another is that both `Type` and `Value` have a `Kind` method that returns a constant indicating what sort of item is stored: `Uint`, `Float64`, `Slice`, and so on. Also methods on `Value` with names like `Int` and `Float` let us grab values (as `int64` and `float64`) stored inside: var x float64 = 3.4 |