From b6b47aa8b22a973280c003145c95acf42b68ccb7 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Tue, 4 Apr 2017 12:43:15 -0700 Subject: 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 --- content/laws-of-reflection.article | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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: +(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 -- cgit v1.2.3