Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape Variables with Printf

Tags:

go

If I wanted to do the following:

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

How could I escape the first occurrence of %v?

\%v doesn't seem to work. Any ideas?

like image 962
Ari Seyhun Avatar asked Feb 28 '16 10:02

Ari Seyhun


People also ask

What is %q in printf?

Characters in the format string are copied to the output or, if a % is encountered, are used to format an item. In addition to the standard formats, %b causes printf to expand backslash escape sequences (for example \n for newline), and %q outputs an item that can be used as shell input.

What is the escape character for fprintf functions?

In some character encodings including BIG5 and GB18030, there are hundreds of characters that contain the encoding of backslash, and to escape those for printf , you'd need to insert a \ before each 0x5c byte within the encoding of those characters!


1 Answers

You can use %% for literal %

%%  a literal percent sign; consumes no value 

https://golang.org/pkg/fmt/

like image 189
YOU Avatar answered Oct 02 '22 07:10

YOU