-
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: converts numeric type to string before rendering characters as hex #34351
Comments
Hey @wti, Thanks for filing this issue. As far as I can tell, this is consistent with the documentation for fmt: https://golang.org/pkg/fmt/.
As Let me know if you agree with that reasoning. |
That a type implementing Stringer calls the String method first for %x is working as documented and wont likely change due to keeping backwards compatibility. It has also come up and was discussed in other reports e.g. #21535. If you want to control the formatting for different verbs. e.g. %s and %x I think implementing the fmt.Formatter interface can be used to let the verb %x print the type as a hex number without modifying other code. Would that work for the use case here? |
Thank you for the quick replies, the links to docs and bug, and the suggestion to implement Some feedback in case it helps others... I noticed that the https://play.golang.org/p/7pkwiNn5Bc4 type mi uint64
type si struct {
A mi
}
func (m mi) String() string {
return "A"
}
// add `(a mi)` to declare on mi, to see verb is `v`
func Format(s fmt.State, verb rune) {
fmt.Printf("Format mi %c\n", verb);
s.Write([]byte("hi"))
}
func main() {
a := mi(0x12)
s := si{a}
fmt.Printf("%%#x %#x\n%%#v %#v\n", a, s)
}
// Output (0x41 is hex 'A')
%#x 0x41
%#v main.si{A:0x12} I suppose Otherwise, implementing one verb in I don't have a good answer for backwards compatibility issue. Aside from existing code base, 'x' is too common a format verb to deprecate without adding confusion. A new verb for the old behavior could provide a migration path, but with awkward trade-off's. As for avoiding this: I can search docs for I'm still not convinced it's the right thing to adopt Thanks for your quickness and patience with something I should have found in the docs myself. |
Sorry, I was fishing for feedback on three points:
Whether I should write a bug or doc request against fmt...
I recognize it's likely best to just try any/all of these and get feedback on the concrete proposal. |
If wording can be improved/clarified you can send a CL to do so. I do not think this would need an extra proposal. Details can be discussed on the CL. Filling bugs/feature proposals for 2. and 3. sounds good to me. It would be good to describe the problem again in the proposal that is meant to be solved. |
Thanks for the feedback! |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?same result with multiple systems
What did you do?
https://play.golang.org/p/MST7hKUpWmd
What did you expect to see?
number printed as hex
What did you see instead?
Stringer rendering of number printed as hex
Discussion
I defined a uint64 type (
type shex uint64
) and made it a Stringer.When I print it using
%x
, the value is converted to string first, and then the characters are converted to hex. I expect it to be treated as a number and converted to hex.A workaround is to cast it back to uint64, but that works only if you write the formatting code.
The code is at print.go:627:
Using Stringer seems reasonable for verbs s, q, and v, but incorrect for x/X. (Indeed, if x is there, why is d not?)
Motivation
I'm using a type defined on uint64 as an encoding of other values, and the String() implementation must render the decoded form. I will not control the code that renders these values, so I cannot convert the values back to uint64 before the hex form is printed. Code using my types should be able to print the numeric form using numeric verbs and the rendered/decoded form using string verbs.
The text was updated successfully, but these errors were encountered: