Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
fmt: differing output of printf %#010x with int and string types #28627
What did you do?
I wanted print a padded (zeros) and prepended (0x) string character as a hex number.
What did you expect to see?
The same result for a string character and an integer.
What did you see instead?
This seems to be consistent with the documentation (https://godoc.org/fmt):
The difference is that the verb %x applies to each byte in strings and # is an alternate format for the verb so also per byte but the padding is not described to be per byte but applies to the output as a whole.
An improvement for the documentation that could be made is for '0':
you are right it i was confusing this with ' ' where it applies per byte and is mentioned explicitly "% x" in documentation.
Referring back to the original issue of 0 padding I think its still in line with documentation that padding of 0 applies to the whole string after it has been formatted? Could maybe made explicit.
To format the string like the number in the first example you could do:
If its not an obvious bug or contradicting documentation its likely not to change how fmt formats this as its established behavior by now that other programs will rely on.
I cannot find this in the documentation for printf.
This is what I've found:
It does only say what happens if you pad it with spaces, so I agree it should be more explicitly stated what happens if you use zeros instead of spaces.