How to print backslash in go?

1.4k views Asked by At

I have the following code snippet:

const byte1 = 0x19;
const byte2 = 0x45;
msg := fmt.Sprintf("\\x%x\\x%x message", byte1, byte2)
log.Info("Learning go fmt", "msg", msg)

I get this:

msg="\\x19\\x45 message"

Why is the backslash duplicated? According to this website, \\ within a format should yield \.

1

There are 1 answers

0
Paul Razvan Berg On BEST ANSWER

@nilsocket's comment is correct. The problem is that I'm using the Ethereum log package. It unescapes the string. If I do:

fmt.Println("\\x%x\\x%x message", byte1, byte2)

It works perfectly fine.