Skip to content

Avoid escape characters and print raw data #1374

Discussion options

You must be logged in to vote

Hi @veera-chenna, I've converted this to a discussion as this doesn't seem to be a bug or feature request.

When I am trying to print the string, it is escaping the \\ and printing only \,

To the contrary, the JSON string contains a \ which is escaped as \\, so gomplate is behaving correctly by un-escaping it as you output the value.

I think what you want to do is quote the string so that it's re-escaped. There are a few ways you could approach this, but one way is to use printf with the %q ("quote") formatter:

For example:

$ gomplate -i '{{ $s := "no\\escape" }}{{ printf "%q" $s }}'
"no\\escape"

You could also use the strings.Quote function, which is equivalent:

$ gomplate -i '{{ $s := …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by veera-chenna
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1372 on April 22, 2022 17:04.