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

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

Linda Hamilton
Release: 2024-11-12 10:58:02
Original
863 people have browsed it

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")
Copy after login

This will print:

Escape this -> %v... Do not escape this -> Unescaped
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template