-
Notifications
You must be signed in to change notification settings - Fork 1.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
Add advice on formatting strings using %q
#186
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Added a few suggestions, but looks good in general.
This advice applies more generally to other contexts when reporting user-specified data, such as logging invalid usernames: | ||
|
||
```go | ||
log.Printf("User %q does not exist", username) | ||
// User "no_name" does not exist | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would generalize this slightly to something like:
This advice can also apply more generally to other formatting situations
when a dynamic string could confuse or change the formatting or meaning of
surrounding text when interpolated.
Mostly to avoid bucketing "logging" as "formatting" (i.e., structured logging - which is at least as relevant as log.Printf
scenarios - doesn't require the same treatment for fields, because they are already discrete values).
I think the log.Printf
example is fine to keep, I would just put a good/bad example under the fmt.Errorf
examples.
@@ -0,0 +1,38 @@ | |||
# Format Strings Using `%q` | |||
|
|||
Whenever formatting messages that contain a string component via `fmt`, use `%q` instead of `%s`. This will wrap the specified string in quotes, helping it stand out from the rest of the error message. More importantly, if the string is empty, it will provide a more helpful error message. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whenever formatting messages that contain a string component via
fmt
, use%q
instead of%s
.
I think we might want to clarify slightly: it's not necessarily just when a string component in general is used, but more when dynamic strings (incl. user-provided values) could potentially cause interpolation to result in something weird.
Maybe something like:
Whenever dynamic strings - such as user-provided data - are used for formatting,
prefer the placeholder%q
over%s
.
@@ -0,0 +1,38 @@ | |||
# Format Strings Using `%q` | |||
|
|||
Whenever formatting messages that contain a string component via `fmt`, use `%q` instead of `%s`. This will wrap the specified string in quotes, helping it stand out from the rest of the error message. More importantly, if the string is empty, it will provide a more helpful error message. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[...] helping it stand out from the rest of the error message.
More importantly, if the string is empty, it will provide a more helpful error message.
I'd either generalize these slightly or remove the word "error" (this guidance can apply to more than just errors). Maybe something like:
More importantly, if the string is empty or has its own formatting, the result
will be clearer and more consistent.
This PR adds suggestions on formatting strings using
%q
over%s
.Source: https://abhinavg.net/2021/12/29/fmt-errof-q/ by @abhinav
Fixes #185