guide
guide copied to clipboard
Proposal: prefer `%q` to format strings over `%s`
Note All credit goes to @abhinav
I learned this from one of @abhinav's amazing talks, and thought we could probably add this practice to this doc.
Whenever formatting messages that contain a string component via fmt
, prefer %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.
Bad | Good |
---|---|
|
|
+1, definitely want to encourage %q
over %s
, and possibly also encouraging using %q
when reporting user-specified data like filenames in errors (vs no quotes).