You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
fmt.Errrof("file %s not found", filename)
// Prints the following: // file myfile.go not found//// Or if the string is empty:// file not found
fmt.Errrof("file %q not found", filename)
// Prints the following:// file "myfile.go" not found//// Or if the string is empty:// file "" not found
The text was updated successfully, but these errors were encountered:
+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).
yuxincs
changed the title
Proposal: prefer %q to format strings instead of %s
Proposal: prefer %q to format strings over %sJun 5, 2023
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.The text was updated successfully, but these errors were encountered: