printf-style function with dynamic format string and no further arguments should use print-style function instead
130func (eng *Engine) announce() *Engine {
131 if eng.config.Debug {
132 eng.announceLock.Do(func() {
133 fmt.Printf(language.Get("goadmin is now running. \nrunning in \"debug\" mode. switch to \"release\" mode in production.\n\n"))134 })
135 }
136 return eng
Description
Using fmt.Printf
with a dynamic first argument can lead to unexpected output.
The first argument is a format string, where certain character combinations have
special meaning.
Forming the first parameter via string
concatenation with user input should be
avoided for the same reason. When printing user input, either use a variant of
fmt.Print
, or use the %s
Printf
verb and pass the string
as an argument.
Bad practice
s := "Interest rate: 5%"
fmt.Printf(s) // Prints: Interest rate: 5%!(NOVERB)
Recommended
s := "Interest rate: 5%"
fmt.Print(s) // Prints: Interest rate: 5%
s := "Interest rate: 5%"
fmt.Printf("%s", s) // Prints: Interest rate: 5%