Navigation Menu

Skip to content
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 %#v doesn't consider String() methods of types #9783

Closed
eloff opened this issue Feb 5, 2015 · 4 comments
Closed

fmt: Printf %#v doesn't consider String() methods of types #9783

eloff opened this issue Feb 5, 2015 · 4 comments

Comments

@eloff
Copy link

eloff commented Feb 5, 2015

I ran into this with an enum type: http://play.golang.org/p/rK_gIV72ea

But itemType defines a String() method which, were it used, would make the Printf a lot more readable. Wasn't String() designed for Printf?

Is there a reason this can't or shouldn't be done? If not I would like to create and contribute a patch.

@eloff eloff changed the title Printf %#v doesn't consider String() string methods of types Printf %#v doesn't consider String() methods of types Feb 5, 2015
@adg
Copy link
Contributor

adg commented Feb 5, 2015

%#v formats the argument as "a Go-syntax representation of the value". A stringer does not produce such a representation. Use %v if you want to invoke a stringer.

@adg adg closed this as completed Feb 5, 2015
@ianlancetaylor
Copy link
Contributor

Note that you can customize %#v output by defining a GoString method.

@eloff
Copy link
Author

eloff commented Feb 5, 2015

Forgot about GoString(), that's what I was looking for. testify uses %#v to produce messages for assert.Equal, which is lousy for enums. But now I have an easy way to fix it. Thanks.

@mikioh mikioh changed the title Printf %#v doesn't consider String() methods of types fmt: Printf %#v doesn't consider String() methods of types Feb 5, 2015
@gopherbot
Copy link

CL https://golang.org/cl/10483 mentions this issue.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants