-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
cmd/vet: Printf format %.T has unrecognized flag #30363
Comments
EDIT: Oh, or did you mean that the dot flag is and should be expected (as for strings), and you really wanted the input truncated to 0 in length and you wanted to produce the empty output and wanted vet to shut up? Perhaps clearer example: https://play.golang.org/p/k7yNgXsiVGX Old-comment: So, the Another thing though, the documentation for flags says:
This seems to be a bit in conflict with the implementation. To me that reads as though |
So clearly fmt accepts e.g., |
This is new behavior. %.3T didn't work in 1.11 (https://play.golang.org/p/WzF7OfLxUiw) but it does work in 1.12. (It treats the type name as a string.) . I do not know why it changed, or whether it was a deliberate change. I'd like to understand that before making any decisions. That said, "%.s" means print the string with zero length, so "%.T" in 1.12 produces no output. Not sure what you're trying to achieve here. |
Tentatively marking as release-blocker for 1.12 just in case we want to revert this behavior change before we're locked into it. |
Behavior of vet or fmt.Printf? It seems like Printf has accepted a precision for %T for a long time:
Vet also complained in 1.10:
|
It worked all the way back in go1.4, the oldest version I have installed. So perhaps it broke in 1.11 only, and its behavior has been restored. No need to block the release over this. |
For my use case, I am trying to truncate the type string to zero (in order words, trying to produce no output) as you mentioned. As a concrete example, I let admins configure a weather alert (https://play.golang.org/p/B1oWVEWxTvy) like this:
Then I can format either string with:
If they don't want to use a string argument they can suppress it with |
(CC @alandonovan @matloob @ianthehat for |
@bryancallahan It is unfortunate that printf provides no clear way to consume and discard argument of any type without printing anything. That said, printing a zero-width type string is clearly a hack, and it would likely get called out during a code review. If you need to dynamically vary the format string and can't do so in logic, the text/template package is the right tool for the job: its template strings are self-documenting. All of which is to say: this may be a bug in vet, but I don't think it's very important. [Update: I was mistaken. Thanks to antong for pointing out that a format string using indexed notation is not subject to the unused parameter check.] |
Aah, so you were trying to suppress the printf error output like Example: https://play.golang.org/p/GqlrZ42nNBF But yes, this sounds like a job for a template package. I don't know how safe text/template is for user controlled templates. |
Much better! |
Very awesome, @antong! Thanks so much for the help and clarification on this. This is exactly what I was going for. @alandonovan in the future I may look into the text/template package. (I started out w/ this approach because I wanted to keep things simple until I had a sufficient requirement to "grow into" the text/template package.) Again, I very much appreciate the clarification and consideration on this you two! 👍 |
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
)?go env
OutputWhat did you do?
https://play.golang.org/p/m0ahZ7ofAH_f
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: