From 3be471932e8ddf3558454bfa7c8a9148d221e75e Mon Sep 17 00:00:00 2001 From: Yuxin Wang Date: Mon, 5 Jun 2023 10:24:49 -0400 Subject: [PATCH 1/2] Add advice on formatting strings using `%q` Source: https://abhinavg.net/2021/12/29/fmt-errof-q/ --- src/SUMMARY.md | 1 + src/formatting-strings.md | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/formatting-strings.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index f3b1027e..f6730343 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -60,6 +60,7 @@ - [Initializing Maps](map-init.md) - [Format Strings outside Printf](printf-const.md) - [Naming Printf-style Functions](printf-name.md) + - [Format Strings Using `%q`](formatting-strings.md) - Patterns - [Test Tables](test-table.md) - [Functional Options](functional-option.md) diff --git a/src/formatting-strings.md b/src/formatting-strings.md new file mode 100644 index 00000000..3e9c7f59 --- /dev/null +++ b/src/formatting-strings.md @@ -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. + + + + + +
BadGood
+ +```go +fmt.Errrof("file %s not found", filename) +// Prints the following: +// file myfile.go not found +// +// Or if the string is empty: +// file not found +``` + + + +```go +fmt.Errrof("file %q not found", filename) +// Prints the following: +// file "myfile.go" not found +// +// Or if the string is empty: +// file "" not found +``` + +
+ +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 +``` From 6b1464c6602f937a756bc93ee333ad6db9efda4b Mon Sep 17 00:00:00 2001 From: yuxincs Date: Mon, 5 Jun 2023 14:28:02 +0000 Subject: [PATCH 2/2] Auto-update style.md --- style.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/style.md b/style.md index a8777d21..17217595 100644 --- a/style.md +++ b/style.md @@ -67,6 +67,7 @@ - [Initializing Maps](#initializing-maps) - [Format Strings outside Printf](#format-strings-outside-printf) - [Naming Printf-style Functions](#naming-printf-style-functions) + - [Format Strings Using `%q`](#format-strings-using-q) - [Patterns](#patterns) - [Test Tables](#test-tables) - [Functional Options](#functional-options) @@ -3584,6 +3585,45 @@ go vet -printfuncs=wrapf,statusf See also [go vet: Printf family check](https://kuzminva.wordpress.com/2017/11/07/go-vet-printf-family-check/). +### 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. + + + + + +
BadGood
+ +```go +fmt.Errrof("file %s not found", filename) +// Prints the following: +// file myfile.go not found +// +// Or if the string is empty: +// file not found +``` + + + +```go +fmt.Errrof("file %q not found", filename) +// Prints the following: +// file "myfile.go" not found +// +// Or if the string is empty: +// file "" not found +``` + +
+ +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 +``` + ## Patterns ### Test Tables