Printing a reflect.Value

197 views
Skip to first unread message

sb...@google.com

unread,
Jun 7, 2016, 6:14:00 PM6/7/16
to golang-nuts
Hello group,


1. If the operand is a reflect.Value, the operand is replaced by the concrete value that it holds, and printing continues with the next rule. 

5. If an operand implements method String() string, that method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any). 

So, in the following program (https://play.golang.org/p/V4H-vkjoE5):

package main

import (
 
"fmt"
 
"reflect"
)

type
SpecialInt int

func
(i SpecialInt) String() string { return fmt.Sprintf("SpecialInt(%d)", int(i)) }

func main
() {
 v
:= reflect.ValueOf(SpecialInt(73))
 fmt
.Printf("%+v\n", v)
 fmt
.Printf("%+v\n", v.Interface())
}

I expect the first fmt.Printf call to substitute the concrete value of v, which is SpecialInt(73), invoke the String() method on it, and print "SpecialInt(73)". However, it just prints "73". On the contrary, the second fmt.Printf call (correctly) prints "SpecialInt(73)".

Is there anything I'm missing here, or maybe something wrong with the Go documentation or implementation?

Thanks.

Matt Harden

unread,
Jun 7, 2016, 10:45:17 PM6/7/16
to sb...@google.com, golang-nuts
This definitely seems inconsistent with the documentation, unless you allow the phrase "concrete value" to mean two different things depending on which part of the doc you're reading. Unfortunately that term is not defined in the godoc or the language specification.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rob Pike

unread,
Jun 8, 2016, 12:20:11 AM6/8/16
to Matt Harden, sb...@google.com, golang-nuts
Please file an issue.

Matt Harden

unread,
Jun 8, 2016, 6:21:04 PM6/8/16
to Rob Pike, sb...@google.com, golang-nuts
Reply all
Reply to author
Forward
0 new messages