Do not use Printf with Sprintf
このページは日本語には対応しておりません。随時翻訳に取り組んでいます。
翻訳に関してご質問やご意見ございましたら、
お気軽にご連絡ください。
ID: go-best-practices/printf-sprintf
Language: Go
Severity: Info
Category: Best Practices
Description
In Go, developers should avoid using fmt.Printf(fmt.Sprintf("something"))
and instead use fmt.Printf("something")
.
Here are a few reasons why:
- Unnecessary complexity: The
fmt.Sprintf
function is used to format a string with placeholders and values. But when you have a simple string like "something"
, there is no need to use fmt.Sprintf
to create it. It adds unnecessary complexity to the code without any benefit. - Performance impact: Using
fmt.Sprintf
to create a string unnecessarily involves extra processing and memory allocation for string formatting. This can have a performance impact, especially if the format string is used frequently or in performance-sensitive areas of the code. - Readability and maintainability: The excessive use of formatting functions can make the code harder to read and understand. It can also introduce opportunities for mistakes or confusion when working with complex format strings.
- Code smell detection: Many code analysis and linter tools can detect unnecessary usage of
fmt.Sprintf
with constant strings and flag it as a code smell or potential issue. This can lead to noise and make it harder to identify genuine issues in the codebase.
By directly using fmt.Printf("something")
, you keep the code simpler, more readable, and avoid unnecessary performance overhead. It ensures that the formatting functions are used when needed, rather than wrapping constant strings in unnecessary formatting constructs.
Non-Compliant Code Examples
func main() {
fmt.Printf(fmt.Sprintf("something"))
}
Compliant Code Examples
func main() {
fmt.Printf("something")
}