Home > Backend Development > Golang > How do I escape the literal '%' in a `Printf` statement?

How do I escape the literal '%' in a `Printf` statement?

Linda Hamilton
Release: 2024-11-12 18:46:01
Original
853 people have browsed it

How do I escape the literal

Escaping Variables in Printf with "%"

In your code, you attempt to escape the first occurrence of "%v" using "%v". However, this does not work as "%" escapes the percent sign, but it doesn't affect the "v" format specifier.

To escape the "%v" literal, you need to use the "%%" escape sequence. The "%%" sequence represents a literal percent sign. It consumes no value from the provided arguments and simply prints a percent sign.

Therefore, to escape the first occurrence of "%v" in your code, you would use the following:

fmt.Printf("Escape this -> %%v... Do not escape this -> %v", "Unescaped")
Copy after login

This will output:

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

As you can see, the first "%v" is escaped and printed as a literal percent sign, while the second "%v" is used to format the "Unescaped" string.

The above is the detailed content of How do I escape the literal '%' in a `Printf` statement?. For more information, please follow other related articles on the PHP Chinese website!

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