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: %x on Stringer gives unintuitive results when value is an integer. #4754

rogpeppe opened this issue Feb 4, 2013 · 2 comments

fmt: %x on Stringer gives unintuitive results when value is an integer. #4754

rogpeppe opened this issue Feb 4, 2013 · 2 comments


Copy link

@rogpeppe rogpeppe commented Feb 4, 2013

d := time.Duration(1024)
    fmt.Printf("decimal: %d; hex %x\n", d, d)

        decimal: 1024; hex 312e3032347573

not the expected hex 400.

i think that fmt should probably print integer
values as integers if used with integer-specific
format specifiers.
Copy link

@rsc rsc commented Feb 4, 2013

Comment 1:

I'm inclined to agree but leaving for Rob to decide.

Labels changed: added priority-later, removed priority-triage.

Owner changed to @robpike.

Status changed to Thinking.

Copy link

@robpike robpike commented Feb 13, 2013

Comment 2:

This is a tough one but I'm inclined to leave things alone. 'x' is an integer format but
it's also a string format and the code says: "If a string is acceptable according to the
format, see if the value satisfies [error or Stringer]".
If 'x' becomes integer in this context, then I need to think about other non-string
formats like %g, and I think it just gets more complicated. Also, making the change
could possibly break code that wants to hexify the String result, which is reasonable
In short, I will mark this WorkingAsIntended because, although the proposal is
reasonable, so is the existing behavior. To get the behavior you ask for, it's easy and
arguably clearer to convert:
fmt.Printf("decimal: %d; hex %x\n", int(d), int(d))

Status changed to WorkingAsIntended.

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.