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: "%%" placeholder accepts incorrect index values #22868
What version of Go are you using (go version)?
Does this issue reproduce with the latest release?
What operating system and processor architecture are you using (go env)?
macOS Sierra 10.12.6, darwin/amd64
What did you do?
Run the following code
What did you expect to see?
In all cases, errors about incorrect index is printed.
What did you see instead?
In all cases, '%' is printed. This behaviour is unexpected and, at least, not documented here:
Since documentation says "%% a literal percent sign; consumes no value" the index for the non consumed value is not checked. This could be explicity documented or easily changed by changing the order in which this switch statement in fmt is evaluated:
However changing this is a backwards incompatible change that can break code (even if unlikely case) that worked fine before.
Speaking about 'consumes no value'.
So if we use index for width or precision, value is consumed and still index is not checked.
And what about indices for width and precision? Value is consumed, so this is not a no-op.
I found another interesting case connected to width and precision:
So if width/precision index is correct, %% behaves like any other verb and consumes corresponding value. If index is incorrect (not a number, < 1, or > arguments number), it consumes the first value.