-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fmt: Printf not respecting String() method of elements of a struct #17409
Comments
This only happens if the field is unexported. If it's exported, String is properly called: |
That was mentioned in the original "golang-nuts" thread quoted earlier. |
works as expected. reflect can not extract the concrete value of an unexported field and thereby fmt cant either to call the String method. related: |
If there are no arguments against i think this can be closed. That the String method of an unexported field can not be called in another package is normal behavior and fmt is another package. In general e.g. somestruct.someunexportedfield.String() is not directly callable in another package. It is possible to define a String method on Box that has access to elem and then hands this over to fmt directly to allow for the unexported fields String method to be called:
|
I wonder if we can document this in some way that makes sense. |
CL https://golang.org/cl/31817 mentions this issue. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go1.7
What operating system and processor architecture are you using (
go env
)?play.golang.org
What did you do?
First reported in https://groups.google.com/d/topic/golang-nuts/Upiw-xxCJxQ/discussion
If an operand of
fmt.Printf
implements methodString() string
,fmt.Printf
respects it and uses it.But it doesn't if an operand is a
struct
that has such an element.https://play.golang.org/p/QJC7Q9Kpch
What did you expect to see?
What did you see instead?
In the second line of the output, none of the format specifiers (
"%v"
,"%+v"
,"%#v"
) calls theString()
method ofInt
, in contrast to the first line.The text was updated successfully, but these errors were encountered: