-
Notifications
You must be signed in to change notification settings - Fork 18k
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: padding flag is not accepted with minus flag #51419
Comments
Context, we have a 13 character fixed width field whose last two characters need to be "00". We can easily work around this, but it was unclear if this behaviour should be expected or not. |
Padding a string with zeros doesn't seem right, but the documentation doesn't say it shouldn't work. Just for reference, as the fmt package was written to broadly follow C's stdio, I tried the equivalent C program, and got these warnings from clang: x.c:6:11: warning: flag '0' results in undefined behavior with 's' conversion specifier [-Wformat] Meanwhile its output is: 123456789 which implies the warnings are largely irrelevant. Yay warnings. So although you're on thin ice here, I'd be willing to accept this as a bug, as long as fixing it doesn't break too many things, which seems plausible. |
I didn't see this existing test which is explicit about rejecting using zeros as right-padding. https://github.com/golang/go/blob/go1.17.7/src/fmt/fmt_test.go#L990-L992 It looks like this was changed as part of a performance improvement, but I'm unsure why it was changed back then. |
It is very odd to right-pad a string with zeros. It's easy to argue it's wrong to expect the library to do it for you. So I'm back on the 'not a bug' side of the fence, perhaps with a line in the documentation about it. |
Agreed. There's been a test rejecting this behavior since 2016, so I'm going to file a change updating the docs. Changing this would break people's code. |
Change https://go.dev/cl/389434 mentions this issue: |
/cc @martisch |
Change https://go.dev/cl/390674 mentions this issue: |
Replace 24 spaces added in CL 389434 with 3 tabs, so the new line is indented like other lines around it. Updates #51419. Change-Id: Ic3e50023a01f233c52dda53c36de2c461222d95c Reviewed-on: https://go-review.googlesource.com/c/go/+/390674 Trust: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Adam Shannon <adamkshannon@gmail.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, verified in the go.dev sandbox.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
It seems that the minus and padding character flags don't work when both are specified. I'm seeing the following format no pad with "0" as specified in the flag.
Example: https://go.dev/play/p/IDEqulgJAwD
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: