Escape Variables with Printf
If you want to prevent the formatting of a variable in a fmt.Printf statement, you can use escape sequences. However, the standard %v escape sequence does not work to escape the first occurrence of %v.
To escape the first occurrence of %v, use %%. This sequence represents a literal percent sign, which does not consume any value.
Here is an example:
fmt.Printf("Escape this -> %%v... Do not escape this -> %v", "Unescaped")
This will print:
Escape this -> %v... Do not escape this -> Unescaped
The first %v is escaped by %%, while the second %v is not. Therefore, the first %v is printed literally, while the second %v is formatted as a string.
The above is the detailed content of How to Escape a Literal Percent Sign in fmt.Printf Statements?. For more information, please follow other related articles on the PHP Chinese website!