wordSmith
wordSmith

Reputation: 3183

Print new line in xml file when \n literally prints \n

I'm trying to write a .xml file in golang and when I try to write to a newline and use \n, it literally prints \n as part of the string.

How can I force a new line to be printed in the file?

Here is what my code looks like so far:

fmt.Fprint(file, "<card>\n")
fmt.Fprintf(file, `<title>title</title>\n`)

and that is printing <card>\n<title>title</title>\n

Upvotes: 1

Views: 704

Answers (1)

publysher
publysher

Reputation: 11372

Actually, it's printing

<card>
<title>title</title>\n

As you can see here.

The reason is that backslashes are not interpolated in raw strings, i.e. strings that are enclosed with `. If you replace your second line with

fmt.Fprintf("<title>title</title>\n")

your program should work as intended.

Upvotes: 2

Related Questions