-
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: %2x/%02x verb prints one element for a zero length []byte #17024
Comments
Not a bug. %02x means that the format is reserving 2 runes of space for the argument, and that that space is 0-filled from the left. Compare with https://play.golang.org/p/QCbJYhHKmz which fills the space with blanks. |
PS: The '0' in %02x is a format flag, it's not part of the width 2. See https://golang.org/pkg/fmt/ and look for "Other flags" . |
That is sort of surprising; the |
@kortschak Hm, it is confusing indeed. I see the following sections in the fmt documentation: For compound operands such as slices and structs, the format applies to the elements of each operand, recursively, not to the operand as a whole. Thus %q will quote each element of a slice of strings, and %6.2f will control formatting for each element of a floating-point array. However, when printing a byte slice with a string-like verb (%s %q %x %X), it is treated identically to a string, as a single item. Not sure that's sufficient. Leaving for @robpike to comment. |
Re-opening. It looks like a bug to me. %d is working fine, %x has inconsistencies. |
cc @martisch |
I just had a look and the change is just the deletion of the condition at https://tip.golang.org/src/fmt/format.go#L357 with associated code readjustment (change the prior condition to be for With the change:
|
There has been confusion around this in the past. @robpike added some tests for it with https://go-review.googlesource.com/#/c/11600/ The behavior for padding is consistent with go1.4 and i rewrote the %x formatting code in "That is sort of surprising; the '0' and ' ' are applied per element when the slice is non-zero length" The padding is always applied to the whole string because of: https://play.golang.org/p/G5bwjqt4SP %d is different from %x on strings and byte slices in that: The %d on a slice is the recursive application: The %x is the special case: And %x for a string has these rules: So it seems consistent to me with the specific interpretation outlined as %d and %x are not both recursive applications of the integer format verbs %d and %x when applied to strings and byte slices. |
Thanks, Martin. TBH, I was just searching this morning for something to debug some byte slices, reading through the |
examples: Sure if others agree that current behavior of padding the whole string should not be changed since it has been the case since at least 1.4? Maybe 2 examples for byteslice formatting with %x under the section "However, when printing a byte slice with a string-like verb (%s %q %x %X), it is treated identically to a string, as a single item." ?
or better adding another sentence for clarification what the effects are? |
Perhaps it's best to document better rather than change the behavior. There are no executable examples in this package, and to provide them is a major undertaking. Maybe just a few words in the existing package documentation would help. |
The fact that []byte is treated like string for the purposes of %s %q %x %X is documented and emphasized multiple times in the existing doc comment. I don't believe adding any new emphasis is going to help here. As @martisch already mentioned:
|
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
)?What did you do?
Run the program at https://play.golang.org/p/p6yew65soY
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: