Home  >  Article  >  Backend Development  >  How to Escape a Literal Percent Sign in fmt.Printf Statements?

How to Escape a Literal Percent Sign in fmt.Printf Statements?

Linda Hamilton
Linda HamiltonOriginal
2024-11-12 10:58:02833browse

How to Escape a Literal Percent Sign in fmt.Printf Statements?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn