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

Closed
rogpeppe opened this issue Feb 4, 2013 · 2 comments
Closed
Milestone

Comments

@rogpeppe
Copy link
Contributor

rogpeppe commented Feb 4, 2013

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

prints:
        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.
@rsc
Copy link
Contributor

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.

@robpike
Copy link
Contributor

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
itself.
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
@rsc rsc unassigned robpike Jun 22, 2022
This issue was closed.
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